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, |
| 395 | int len, int C, int isTransient, int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 396 | { |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 397 | int i; |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 398 | VARDECL(int, metric); |
| 399 | int cost0; |
| 400 | int cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 401 | VARDECL(int, path0); |
| 402 | VARDECL(int, path1); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 403 | VARDECL(celt_norm, tmp); |
| 404 | int lambda; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 405 | int tf_select=0; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 406 | SAVE_STACK; |
| 407 | |
Jean-Marc Valin | 6bf3b0a | 2010-07-19 14:32:40 -0400 | [diff] [blame] | 408 | /* FIXME: Should check number of bytes *left* */ |
| 409 | if (nbCompressedBytes<15*C) |
| 410 | { |
| 411 | for (i=0;i<len;i++) |
| 412 | tf_res[i] = 0; |
| 413 | return 0; |
| 414 | } |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 415 | if (nbCompressedBytes<40) |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 416 | lambda = 10; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 417 | else if (nbCompressedBytes<60) |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 418 | lambda = 4; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 419 | else if (nbCompressedBytes<100) |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 420 | lambda = 2; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 421 | else |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 422 | lambda = 1; |
Jean-Marc Valin | 7331977 | 2010-05-28 21:12:39 -0400 | [diff] [blame] | 423 | |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 424 | ALLOC(metric, len, int); |
| 425 | 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] | 426 | ALLOC(path0, len, int); |
| 427 | ALLOC(path1, len, int); |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 428 | |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 429 | for (i=0;i<len;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 430 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 431 | int j, k, N; |
| 432 | celt_word32 L1, best_L1; |
| 433 | int best_level=0; |
| 434 | N = (m->eBands[i+1]-m->eBands[i])<<LM; |
| 435 | for (j=0;j<N;j++) |
| 436 | tmp[j] = X[j+(m->eBands[i]<<LM)]; |
| 437 | if (C==2) |
| 438 | for (j=0;j<N;j++) |
| 439 | tmp[j] = ADD16(tmp[j],X[N0+j+(m->eBands[i]<<LM)]); |
| 440 | L1=0; |
| 441 | for (j=0;j<N;j++) |
| 442 | L1 += ABS16(tmp[j]); |
| 443 | /* Biasing towards better freq resolution (because of spreading) */ |
| 444 | if (isTransient) |
| 445 | L1 += MULT16_32_Q15(QCONST16(.08,15), L1); |
| 446 | else |
| 447 | L1 -= MULT16_32_Q15(QCONST16(.08,15), L1); |
| 448 | best_L1 = L1; |
| 449 | /*printf ("%f ", L1);*/ |
| 450 | for (k=0;k<LM;k++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 451 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 452 | if (isTransient) |
| 453 | haar1(tmp, N>>(LM-k), 1<<(LM-k)); |
| 454 | else |
| 455 | haar1(tmp, N>>k, 1<<k); |
| 456 | |
| 457 | L1=0; |
| 458 | for (j=0;j<N;j++) |
| 459 | L1 += ABS16(tmp[j]); |
| 460 | |
| 461 | /*printf ("%f ", L1);*/ |
| 462 | if (L1 < best_L1) |
| 463 | { |
| 464 | best_L1 = L1; |
| 465 | best_level = k+1; |
| 466 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 467 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 468 | /*printf ("%d ", isTransient ? LM-best_level : best_level);*/ |
| 469 | if (isTransient) |
| 470 | metric[i] = best_level; |
| 471 | else |
| 472 | metric[i] = -best_level; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 473 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 474 | /*printf("\n");*/ |
| 475 | /* FIXME: Figure out how to set this */ |
| 476 | tf_select = 1; |
| 477 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 478 | cost0 = 0; |
| 479 | cost1 = lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 480 | /* Viterbi forward pass */ |
| 481 | for (i=1;i<len;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 482 | { |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 483 | int curr0, curr1; |
| 484 | int from0, from1; |
Jean-Marc Valin | 581fdba | 2010-05-28 06:56:23 -0400 | [diff] [blame] | 485 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 486 | from0 = cost0; |
| 487 | from1 = cost1 + lambda; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 488 | if (from0 < from1) |
| 489 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 490 | curr0 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 491 | path0[i]= 0; |
| 492 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 493 | curr0 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 494 | path0[i]= 1; |
| 495 | } |
| 496 | |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 497 | from0 = cost0 + lambda; |
| 498 | from1 = cost1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 499 | if (from0 < from1) |
| 500 | { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 501 | curr1 = from0; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 502 | path1[i]= 0; |
| 503 | } else { |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 504 | curr1 = from1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 505 | path1[i]= 1; |
| 506 | } |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 507 | cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]); |
| 508 | 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] | 509 | } |
Jean-Marc Valin | 8823261 | 2010-05-28 18:01:02 -0400 | [diff] [blame] | 510 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; |
Jean-Marc Valin | 9d420c5 | 2010-05-28 17:26:27 -0400 | [diff] [blame] | 511 | /* Viterbi backward pass to check the decisions */ |
| 512 | for (i=len-2;i>=0;i--) |
| 513 | { |
| 514 | if (tf_res[i+1] == 1) |
| 515 | tf_res[i] = path1[i+1]; |
| 516 | else |
| 517 | tf_res[i] = path0[i+1]; |
| 518 | } |
Jean-Marc Valin | 71ae6d4 | 2010-06-27 21:55:08 -0400 | [diff] [blame] | 519 | RESTORE_STACK; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 520 | return tf_select; |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 521 | } |
| 522 | |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 523 | 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] | 524 | { |
| 525 | int curr, i; |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 526 | ec_enc_bit_prob(enc, tf_res[start], isTransient ? 16384 : 4096); |
| 527 | curr = tf_res[start]; |
| 528 | for (i=start+1;i<end;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 529 | { |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 530 | ec_enc_bit_prob(enc, tf_res[i] ^ curr, isTransient ? 4096 : 2048); |
| 531 | curr = tf_res[i]; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 532 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 533 | ec_enc_bits(enc, tf_select, 1); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 534 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 535 | 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^] | 536 | /*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] | 537 | } |
| 538 | |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 539 | 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] | 540 | { |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 541 | int i, curr, tf_select; |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 542 | tf_res[start] = ec_dec_bit_prob(dec, isTransient ? 16384 : 4096); |
| 543 | curr = tf_res[start]; |
| 544 | for (i=start+1;i<end;i++) |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 545 | { |
Jean-Marc Valin | a8160dd | 2010-10-12 14:55:16 -0400 | [diff] [blame] | 546 | tf_res[i] = ec_dec_bit_prob(dec, isTransient ? 4096 : 2048) ^ curr; |
| 547 | curr = tf_res[i]; |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 548 | } |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 549 | tf_select = ec_dec_bits(dec, 1); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 550 | for (i=start;i<end;i++) |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 551 | 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] | 552 | } |
| 553 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 554 | #ifdef FIXED_POINT |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 555 | 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] | 556 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 557 | #else |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 558 | 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] | 559 | { |
| 560 | #endif |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 561 | int i, c, N, NN; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 562 | int bits; |
Jean-Marc Valin | bc79991 | 2008-11-07 22:53:13 -0500 | [diff] [blame] | 563 | int has_fold=1; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 564 | ec_byte_buffer buf; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 565 | ec_enc _enc; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 566 | VARDECL(celt_sig, in); |
| 567 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 568 | VARDECL(celt_norm, X); |
| 569 | VARDECL(celt_ener, bandE); |
| 570 | VARDECL(celt_word16, bandLogE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 571 | VARDECL(int, fine_quant); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 572 | VARDECL(celt_word16, error); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 573 | VARDECL(int, pulses); |
| 574 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 575 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 576 | VARDECL(int, tf_res); |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 577 | celt_sig *_overlap_mem; |
| 578 | celt_word16 *oldBandE; |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 579 | int shortBlocks=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 580 | int isTransient=0; |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 581 | int transient_time, transient_time_quant; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 582 | int transient_shift; |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 583 | int resynth; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 584 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 585 | int mdct_weight_shift = 0; |
| 586 | int mdct_weight_pos=0; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 587 | int LM, M; |
Jean-Marc Valin | 890a9c0 | 2010-06-13 08:06:28 -0400 | [diff] [blame] | 588 | int tf_select; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 589 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 590 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 591 | int codedBands; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 592 | int alloc_trim; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 593 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 594 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 595 | if (nbCompressedBytes<0 || pcm==NULL) |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 596 | return CELT_BAD_ARG; |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 597 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 598 | for (LM=0;LM<4;LM++) |
| 599 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 600 | break; |
| 601 | if (LM>=MAX_CONFIG_SIZES) |
| 602 | return CELT_BAD_ARG; |
| 603 | M=1<<LM; |
Jean-Marc Valin | 8679a80 | 2008-10-18 07:44:35 -0400 | [diff] [blame] | 604 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 605 | _overlap_mem = st->in_mem+C*(st->overlap); |
| 606 | oldBandE = (celt_word16*)(st->in_mem+2*C*(st->overlap)); |
| 607 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 608 | if (enc==NULL) |
| 609 | { |
| 610 | ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes); |
| 611 | ec_enc_init(&_enc,&buf); |
| 612 | enc = &_enc; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 613 | nbFilledBytes=0; |
| 614 | } else { |
Jean-Marc Valin | bdcaaf7 | 2010-07-05 13:52:41 -0400 | [diff] [blame] | 615 | nbFilledBytes=(ec_enc_tell(enc, 0)+4)>>3; |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 616 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 617 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; |
| 618 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 619 | effEnd = st->end; |
| 620 | if (effEnd > st->mode->effEBands) |
| 621 | effEnd = st->mode->effEBands; |
| 622 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 623 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 624 | ALLOC(in, C*(N+st->overlap), celt_sig); |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 625 | |
Jean-Marc Valin | bdb5883 | 2008-04-20 17:42:10 +1000 | [diff] [blame] | 626 | CELT_COPY(in, st->in_mem, C*st->overlap); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 627 | for (c=0;c<C;c++) |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 628 | { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 629 | const celt_word16 * restrict pcmp = pcm+c; |
| 630 | celt_sig * restrict inp = in+C*st->overlap+c; |
Jean-Marc Valin | b886ddc | 2008-03-25 14:38:55 +1100 | [diff] [blame] | 631 | for (i=0;i<N;i++) |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 632 | { |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 633 | /* Apply pre-emphasis */ |
Jean-Marc Valin | af1fce9 | 2010-07-16 11:05:06 -0400 | [diff] [blame] | 634 | celt_sig tmp = MULT16_16(st->mode->preemph[2], SCALEIN(*pcmp)); |
| 635 | *inp = tmp + st->preemph_memE[c]; |
| 636 | st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp) |
| 637 | - MULT16_32_Q15(st->mode->preemph[0], tmp); |
Jean-Marc Valin | 6229006 | 2008-04-20 22:16:02 +1000 | [diff] [blame] | 638 | inp += C; |
| 639 | pcmp += C; |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 640 | } |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 641 | } |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 642 | CELT_COPY(st->in_mem, in+C*N, C*st->overlap); |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 643 | |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 644 | /* Transient handling */ |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 645 | transient_time = -1; |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 646 | transient_time_quant = -1; |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 647 | transient_shift = 0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 648 | isTransient = 0; |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 649 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 650 | resynth = optional_resynthesis!=NULL; |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 651 | |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 652 | if (st->complexity > 1 && LM>0) |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 653 | { |
| 654 | isTransient = M > 1 && |
| 655 | transient_analysis(in, N+st->overlap, C, &transient_time, |
| 656 | &transient_shift, &st->frame_max, st->overlap); |
| 657 | } else { |
| 658 | isTransient = 0; |
| 659 | } |
| 660 | if (isTransient) |
Jean-Marc Valin | 9375aa4 | 2008-06-27 07:57:35 +1000 | [diff] [blame] | 661 | { |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 662 | #ifndef FIXED_POINT |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 663 | float gain_1; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 664 | #endif |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 665 | /* Apply the inverse shaping window */ |
| 666 | if (transient_shift) |
| 667 | { |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 668 | transient_time_quant = transient_time*(celt_int32)8000/st->mode->Fs; |
| 669 | transient_time = transient_time_quant*(celt_int32)st->mode->Fs/8000; |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 670 | #ifdef FIXED_POINT |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 671 | for (c=0;c<C;c++) |
| 672 | for (i=0;i<16;i++) |
| 673 | 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]); |
| 674 | for (c=0;c<C;c++) |
| 675 | for (i=transient_time;i<N+st->overlap;i++) |
| 676 | in[C*i+c] = SHR32(in[C*i+c], transient_shift); |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 677 | #else |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 678 | for (c=0;c<C;c++) |
| 679 | for (i=0;i<16;i++) |
| 680 | 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] | 681 | gain_1 = 1.f/(1<<transient_shift); |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 682 | for (c=0;c<C;c++) |
| 683 | for (i=transient_time;i<N+st->overlap;i++) |
| 684 | in[C*i+c] *= gain_1; |
Jean-Marc Valin | 7028d62 | 2008-07-10 23:06:58 -0400 | [diff] [blame] | 685 | #endif |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 686 | } |
Jean-Marc Valin | 2014ca3 | 2009-06-18 23:33:04 -0400 | [diff] [blame] | 687 | has_fold = 1; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 688 | } |
Jean-Marc Valin | c5f2a9d | 2008-10-26 22:00:26 -0400 | [diff] [blame] | 689 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 690 | if (isTransient) |
| 691 | shortBlocks = M; |
| 692 | else |
| 693 | shortBlocks = 0; |
| 694 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 695 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 696 | ALLOC(bandE,st->mode->nbEBands*C, celt_ener); |
| 697 | ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16); |
Jean-Marc Valin | 32ec58c | 2009-05-01 21:28:58 -0400 | [diff] [blame] | 698 | /* Compute MDCTs */ |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 699 | compute_mdcts(st->mode, shortBlocks, in, freq, C, LM); |
Jean-Marc Valin | 08a82ff | 2009-06-14 14:05:19 -0400 | [diff] [blame] | 700 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 701 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
Jean-Marc Valin | 8d4ac15 | 2008-02-29 17:24:02 +1100 | [diff] [blame] | 702 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 703 | compute_band_energies(st->mode, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 504fb3c | 2010-08-06 15:56:22 -0400 | [diff] [blame] | 704 | |
| 705 | amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 706 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 707 | /* Band normalisation */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 708 | normalise_bands(st->mode, freq, X, bandE, effEnd, C, M); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 709 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 710 | NN = M*st->mode->eBands[effEnd]; |
Jean-Marc Valin | 9f89cab | 2010-05-21 14:18:38 -0400 | [diff] [blame] | 711 | if (shortBlocks && !transient_shift) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 712 | { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 713 | 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] | 714 | int m; |
| 715 | for (c=0;c<C;c++) |
| 716 | { |
| 717 | m=0; |
| 718 | do { |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 719 | celt_word32 tmp=0; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 720 | for (i=m+c*N;i<c*N+NN;i+=M) |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 721 | tmp += ABS32(X[i]); |
| 722 | sum[m++] += tmp; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 723 | } while (m<M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 724 | } |
| 725 | m=0; |
| 726 | #ifdef FIXED_POINT |
| 727 | do { |
| 728 | if (SHR32(sum[m+1],3) > sum[m]) |
| 729 | { |
| 730 | mdct_weight_shift=2; |
| 731 | mdct_weight_pos = m; |
| 732 | } else if (SHR32(sum[m+1],1) > sum[m] && mdct_weight_shift < 2) |
| 733 | { |
| 734 | mdct_weight_shift=1; |
| 735 | mdct_weight_pos = m; |
| 736 | } |
| 737 | m++; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 738 | } while (m<M-1); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 739 | #else |
| 740 | do { |
| 741 | if (sum[m+1] > 8*sum[m]) |
| 742 | { |
| 743 | mdct_weight_shift=2; |
| 744 | mdct_weight_pos = m; |
| 745 | } else if (sum[m+1] > 2*sum[m] && mdct_weight_shift < 2) |
| 746 | { |
| 747 | mdct_weight_shift=1; |
| 748 | mdct_weight_pos = m; |
| 749 | } |
| 750 | m++; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 751 | } while (m<M-1); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 752 | #endif |
| 753 | if (mdct_weight_shift) |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 754 | 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] | 755 | } |
| 756 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 757 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 758 | /* Needs to be before coarse energy quantization because otherwise the energy gets modified */ |
Jean-Marc Valin | 0f8fc0b | 2010-10-12 23:25:58 -0400 | [diff] [blame^] | 759 | tf_select = tf_analysis(st->mode, bandLogE, oldBandE, effEnd, C, isTransient, tf_res, nbAvailableBytes, X, N, LM); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 760 | for (i=effEnd;i<st->end;i++) |
| 761 | tf_res[i] = tf_res[effEnd-1]; |
| 762 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 763 | ALLOC(error, C*st->mode->nbEBands, celt_word16); |
Jean-Marc Valin | 5e7f02d | 2010-08-08 09:48:22 -0400 | [diff] [blame] | 764 | quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE, |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 765 | oldBandE, nbCompressedBytes*8, st->mode->prob, |
Jean-Marc Valin | 1b36d6c | 2010-08-31 17:21:52 -0400 | [diff] [blame] | 766 | error, enc, C, LM, nbAvailableBytes, st->force_intra, |
| 767 | &st->delayedIntra, st->complexity >= 4); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 768 | |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 769 | if (LM > 0) |
| 770 | ec_enc_bit_prob(enc, shortBlocks!=0, 8192); |
Jean-Marc Valin | 4c77ea9 | 2009-09-14 22:50:41 -0400 | [diff] [blame] | 771 | |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 772 | if (shortBlocks) |
| 773 | { |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 774 | if (transient_shift) |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 775 | { |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 776 | 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] | 777 | ec_enc_uint(enc, transient_shift, 4); |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 778 | ec_enc_uint(enc, transient_time_quant, max_time); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 779 | } else { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 780 | ec_enc_uint(enc, mdct_weight_shift, 4); |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 781 | if (mdct_weight_shift && M!=2) |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 782 | ec_enc_uint(enc, mdct_weight_pos, M-1); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 783 | } |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 784 | } |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 785 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 786 | tf_encode(st->start, st->end, isTransient, tf_res, nbAvailableBytes, LM, tf_select, enc); |
| 787 | |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 788 | if (shortBlocks || st->complexity < 3) |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 789 | { |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 790 | if (st->complexity == 0) |
| 791 | { |
| 792 | has_fold = 0; |
| 793 | st->fold_decision = 3; |
| 794 | } else { |
| 795 | has_fold = 1; |
| 796 | st->fold_decision = 1; |
| 797 | } |
Jean-Marc Valin | 1d17b9a | 2010-08-31 14:51:58 -0400 | [diff] [blame] | 798 | } else { |
| 799 | has_fold = folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, effEnd, C, M); |
| 800 | } |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 801 | ec_enc_bit_prob(enc, has_fold>>1, 8192); |
| 802 | ec_enc_bit_prob(enc, has_fold&1, (has_fold>>1) ? 32768 : 49152); |
| 803 | |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 804 | ALLOC(offsets, st->mode->nbEBands, int); |
| 805 | |
| 806 | for (i=0;i<st->mode->nbEBands;i++) |
| 807 | offsets[i] = 0; |
| 808 | /* Dynamic allocation code */ |
| 809 | /* Make sure that dynamic allocation can't make us bust the budget */ |
| 810 | if (nbCompressedBytes > 30) |
| 811 | { |
| 812 | int t1, t2; |
| 813 | if (LM <= 1) |
| 814 | { |
| 815 | t1 = 3; |
| 816 | t2 = 5; |
| 817 | } else { |
| 818 | t1 = 2; |
| 819 | t2 = 4; |
| 820 | } |
| 821 | for (i=1;i<st->mode->nbEBands-1;i++) |
| 822 | { |
| 823 | if (2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1] > SHL16(t1,DB_SHIFT)) |
| 824 | offsets[i] += 1; |
| 825 | if (2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1] > SHL16(t2,DB_SHIFT)) |
| 826 | offsets[i] += 1; |
| 827 | } |
| 828 | } |
| 829 | for (i=0;i<st->mode->nbEBands;i++) |
| 830 | { |
| 831 | int j; |
| 832 | ec_enc_bit_prob(enc, offsets[i]!=0, 1024); |
| 833 | if (offsets[i]!=0) |
| 834 | { |
| 835 | for (j=0;j<offsets[i]-1;j++) |
| 836 | ec_enc_bit_prob(enc, 1, 32768); |
| 837 | ec_enc_bit_prob(enc, 0, 32768); |
| 838 | } |
| 839 | offsets[i] *= (6<<BITRES); |
| 840 | } |
| 841 | { |
| 842 | int trim_index = 3; |
| 843 | |
| 844 | /*if (isTransient) |
| 845 | trim_index--; |
| 846 | if (has_fold==0) |
| 847 | trim_index--; |
| 848 | if (C==2) |
| 849 | trim_index--;*/ |
| 850 | alloc_trim = trim_coef[trim_index]; |
| 851 | ec_encode_bin(enc, trim_cdf[trim_index], trim_cdf[trim_index+1], 7); |
| 852 | } |
| 853 | |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 854 | /* Variable bitrate */ |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 855 | if (st->vbr_rate_norm>0) |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 856 | { |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 857 | celt_word16 alpha; |
Jean-Marc Valin | 25767d1 | 2009-10-21 23:24:18 -0400 | [diff] [blame] | 858 | celt_int32 delta; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 859 | /* The target rate in 16th bits per frame */ |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 860 | celt_int32 vbr_rate; |
| 861 | celt_int32 target; |
| 862 | celt_int32 vbr_bound, max_allowed; |
| 863 | |
| 864 | vbr_rate = M*st->vbr_rate_norm; |
| 865 | |
| 866 | /* Computes the max bit-rate allowed in VBR more to avoid busting the budget */ |
| 867 | vbr_bound = vbr_rate; |
| 868 | max_allowed = (vbr_rate + vbr_bound - st->vbr_reservoir)>>(BITRES+3); |
| 869 | if (max_allowed < 4) |
| 870 | max_allowed = 4; |
| 871 | if (max_allowed < nbAvailableBytes) |
| 872 | nbAvailableBytes = max_allowed; |
| 873 | target=vbr_rate; |
| 874 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 875 | /* Shortblocks get a large boost in bitrate, but since they |
| 876 | are uncommon long blocks are not greatly effected */ |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 877 | if (shortBlocks) |
| 878 | target*=2; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 879 | else if (M > 1) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 880 | target-=(target+14)/28; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 881 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 882 | /* The average energy is removed from the target and the actual |
| 883 | energy added*/ |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 884 | target=target+st->vbr_offset-588+ec_enc_tell(enc, BITRES); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 885 | |
| 886 | /* 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] | 887 | target=IMIN(nbAvailableBytes,target); |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 888 | /* Make the adaptation coef (alpha) higher at the beginning */ |
| 889 | if (st->vbr_count < 990) |
| 890 | { |
| 891 | st->vbr_count++; |
| 892 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+10),16)); |
| 893 | /*printf ("%d %d\n", st->vbr_count+10, alpha);*/ |
| 894 | } else |
| 895 | alpha = QCONST16(.001f,15); |
| 896 | |
| 897 | /* By how much did we "miss" the target on that frame */ |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 898 | delta = (8<<BITRES)*(celt_int32)target - vbr_rate; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 899 | /* How many bits have we used in excess of what we're allowed */ |
| 900 | st->vbr_reservoir += delta; |
| 901 | /*printf ("%d\n", st->vbr_reservoir);*/ |
| 902 | |
| 903 | /* 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] | 904 | 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] | 905 | st->vbr_offset = -st->vbr_drift; |
| 906 | /*printf ("%d\n", st->vbr_drift);*/ |
| 907 | |
| 908 | /* 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] | 909 | if (st->vbr_reservoir < 0) |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 910 | { |
| 911 | /* We're under the min value -- increase rate */ |
| 912 | int adjust = 1-(st->vbr_reservoir-1)/(8<<BITRES); |
| 913 | st->vbr_reservoir += adjust*(8<<BITRES); |
Jean-Marc Valin | 45f1110 | 2009-10-22 00:12:31 -0400 | [diff] [blame] | 914 | target += adjust; |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 915 | /*printf ("+%d\n", adjust);*/ |
| 916 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 917 | if (target < nbAvailableBytes) |
| 918 | nbAvailableBytes = target; |
| 919 | nbCompressedBytes = nbAvailableBytes + nbFilledBytes; |
| 920 | |
Jean-Marc Valin | bd5d54a | 2009-10-20 00:25:31 -0400 | [diff] [blame] | 921 | /* 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] | 922 | ec_byte_shrink(&buf, nbCompressedBytes); |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 923 | } |
| 924 | |
Jean-Marc Valin | be04f5a | 2010-08-07 21:42:03 -0400 | [diff] [blame] | 925 | /* Bit allocation */ |
| 926 | ALLOC(fine_quant, st->mode->nbEBands, int); |
| 927 | ALLOC(pulses, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 928 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | cb7a2a3 | 2008-02-11 16:44:48 +1100 | [diff] [blame] | 929 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 930 | bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 931 | 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] | 932 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 933 | 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] | 934 | |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 935 | #ifdef MEASURE_NORM_MSE |
Jean-Marc Valin | 4409224 | 2010-07-29 18:32:54 +0200 | [diff] [blame] | 936 | float X0[3000]; |
| 937 | float bandE0[60]; |
| 938 | for (c=0;c<C;c++) |
| 939 | for (i=0;i<N;i++) |
| 940 | X0[i+c*N] = X[i+c*N]; |
| 941 | for (i=0;i<C*st->mode->nbEBands;i++) |
Jean-Marc Valin | 7b5a086 | 2010-07-29 15:01:24 +0200 | [diff] [blame] | 942 | bandE0[i] = bandE[i]; |
| 943 | #endif |
| 944 | |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 945 | /* Residual quantisation */ |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 946 | 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] | 947 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 948 | 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] | 949 | |
Jean-Marc Valin | 37ab9c6 | 2008-11-08 09:14:38 -0500 | [diff] [blame] | 950 | /* Re-synthesis of the coded audio if required */ |
Jean-Marc Valin | b8ba70c | 2010-04-18 22:10:24 -0400 | [diff] [blame] | 951 | if (resynth) |
Jean-Marc Valin | 18ddc02 | 2008-02-22 14:24:50 +1100 | [diff] [blame] | 952 | { |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 953 | VARDECL(celt_sig, _out_mem); |
| 954 | celt_sig *out_mem[2]; |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 955 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 956 | |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 957 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 958 | |
| 959 | #ifdef MEASURE_NORM_MSE |
| 960 | measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C); |
| 961 | #endif |
| 962 | |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 963 | if (mdct_weight_shift) |
| 964 | { |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 965 | 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] | 966 | } |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 967 | |
| 968 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 969 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 970 | |
Jean-Marc Valin | 3b0df0d | 2010-07-16 15:55:30 -0400 | [diff] [blame] | 971 | for (c=0;c<C;c++) |
| 972 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 973 | freq[c*N+i] = 0; |
| 974 | for (c=0;c<C;c++) |
| 975 | for (i=M*st->mode->eBands[st->end];i<N;i++) |
| 976 | freq[c*N+i] = 0; |
| 977 | |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 978 | ALLOC(_out_mem, C*N, celt_sig); |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 979 | |
| 980 | for (c=0;c<C;c++) |
| 981 | { |
| 982 | overlap_mem[c] = _overlap_mem + c*st->overlap; |
| 983 | out_mem[c] = _out_mem+c*N; |
| 984 | } |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 985 | |
| 986 | compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, |
Jean-Marc Valin | 0d8eaa3 | 2010-08-27 14:57:37 -0400 | [diff] [blame] | 987 | transient_shift, out_mem, overlap_mem, C, LM); |
Jean-Marc Valin | e12017e | 2009-10-03 13:57:31 -0400 | [diff] [blame] | 988 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 989 | /* De-emphasis and put everything back at the right place |
| 990 | in the synthesis history */ |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 991 | if (optional_resynthesis != NULL) { |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 992 | 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] | 993 | |
Jean-Marc Valin | 6f7e83d | 2007-12-01 00:36:41 +1100 | [diff] [blame] | 994 | } |
Jean-Marc Valin | d9b9565 | 2008-08-31 23:34:47 -0400 | [diff] [blame] | 995 | } |
Gregory Maxwell | 54547f1 | 2009-02-16 18:56:44 -0500 | [diff] [blame] | 996 | |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 997 | /* If there's any room left (can only happen for very high rates), |
| 998 | fill it with zeros */ |
Jean-Marc Valin | 5d774e0 | 2010-08-04 17:17:18 -0400 | [diff] [blame] | 999 | while (ec_enc_tell(enc,0) + 8 <= nbCompressedBytes*8) |
Jean-Marc Valin | 30d5125 | 2010-06-21 17:55:28 -0400 | [diff] [blame] | 1000 | ec_enc_bits(enc, 0, 8); |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1001 | ec_enc_done(enc); |
Jean-Marc Valin | c871c8d | 2009-06-09 00:57:00 -0400 | [diff] [blame] | 1002 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1003 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1004 | if (ec_enc_get_error(enc)) |
Jean-Marc Valin | 9d785af | 2010-07-18 09:42:05 -0400 | [diff] [blame] | 1005 | return CELT_CORRUPTED_DATA; |
| 1006 | else |
| 1007 | return nbCompressedBytes; |
Jean-Marc Valin | 8ebd345 | 2007-11-29 20:17:32 +1100 | [diff] [blame] | 1008 | } |
| 1009 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1010 | #ifdef FIXED_POINT |
| 1011 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1012 | 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] | 1013 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1014 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1015 | VARDECL(celt_int16, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1016 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1017 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1018 | if (pcm==NULL) |
| 1019 | return CELT_BAD_ARG; |
| 1020 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1021 | for (LM=0;LM<4;LM++) |
| 1022 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1023 | break; |
| 1024 | if (LM>=MAX_CONFIG_SIZES) |
| 1025 | return CELT_BAD_ARG; |
| 1026 | M=1<<LM; |
| 1027 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1028 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1029 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1030 | ALLOC(in, C*N, celt_int16); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1031 | |
| 1032 | for (j=0;j<C*N;j++) |
| 1033 | in[j] = FLOAT2INT16(pcm[j]); |
| 1034 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1035 | if (optional_resynthesis != NULL) { |
| 1036 | 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] | 1037 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1038 | optional_resynthesis[j]=in[j]*(1.f/32768.f); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1039 | } else { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1040 | 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] | 1041 | } |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1042 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1043 | return ret; |
| 1044 | |
| 1045 | } |
| 1046 | #endif /*DISABLE_FLOAT_API*/ |
| 1047 | #else |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1048 | 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] | 1049 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1050 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1051 | VARDECL(celt_sig, in); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1052 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1053 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1054 | if (pcm==NULL) |
| 1055 | return CELT_BAD_ARG; |
| 1056 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1057 | for (LM=0;LM<4;LM++) |
| 1058 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1059 | break; |
| 1060 | if (LM>=MAX_CONFIG_SIZES) |
| 1061 | return CELT_BAD_ARG; |
| 1062 | M=1<<LM; |
| 1063 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1064 | C=CHANNELS(st->channels); |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1065 | N=M*st->mode->shortMdctSize; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1066 | ALLOC(in, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1067 | for (j=0;j<C*N;j++) { |
| 1068 | in[j] = SCALEOUT(pcm[j]); |
| 1069 | } |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1070 | |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1071 | if (optional_resynthesis != NULL) { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1072 | 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] | 1073 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1074 | optional_resynthesis[j] = FLOAT2INT16(in[j]); |
Gregory Maxwell | 8259531 | 2008-09-30 18:20:14 -0400 | [diff] [blame] | 1075 | } else { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1076 | 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] | 1077 | } |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1078 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1079 | return ret; |
| 1080 | } |
| 1081 | #endif |
Jean-Marc Valin | 6e9058a | 2007-12-07 14:59:06 +1100 | [diff] [blame] | 1082 | |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1083 | int celt_encode(CELTEncoder * restrict st, const celt_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1084 | { |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1085 | 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] | 1086 | } |
| 1087 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1088 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1089 | int celt_encode_float(CELTEncoder * restrict st, const float * 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_float(st, pcm, NULL, frame_size, compressed, nbCompressedBytes, NULL); |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1092 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1093 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | d56c610 | 2010-05-07 20:30:22 -0400 | [diff] [blame] | 1094 | |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1095 | int celt_encode_resynthesis(CELTEncoder * restrict st, const celt_int16 * pcm, celt_int16 * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1096 | { |
| 1097 | return celt_encode_with_ec(st, pcm, optional_resynthesis, frame_size, compressed, nbCompressedBytes, NULL); |
| 1098 | } |
| 1099 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1100 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1101 | int celt_encode_resynthesis_float(CELTEncoder * restrict st, const float * pcm, float * optional_resynthesis, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
| 1102 | { |
| 1103 | return celt_encode_with_ec_float(st, pcm, optional_resynthesis, frame_size, compressed, nbCompressedBytes, NULL); |
| 1104 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1105 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | 4794f2e | 2010-05-19 11:56:57 -0400 | [diff] [blame] | 1106 | |
| 1107 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1108 | int celt_encoder_ctl(CELTEncoder * restrict st, int request, ...) |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1109 | { |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1110 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1111 | |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1112 | va_start(ap, request); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1113 | switch (request) |
| 1114 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 1115 | case CELT_GET_MODE_REQUEST: |
| 1116 | { |
| 1117 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1118 | if (value==0) |
| 1119 | goto bad_arg; |
| 1120 | *value=st->mode; |
| 1121 | } |
| 1122 | break; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1123 | case CELT_SET_COMPLEXITY_REQUEST: |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1124 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1125 | int value = va_arg(ap, celt_int32); |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1126 | if (value<0 || value>10) |
| 1127 | goto bad_arg; |
Jean-Marc Valin | 1213ba5 | 2010-08-31 17:03:13 -0400 | [diff] [blame] | 1128 | st->complexity = value; |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1129 | } |
| 1130 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1131 | case CELT_SET_START_BAND_REQUEST: |
| 1132 | { |
| 1133 | celt_int32 value = va_arg(ap, celt_int32); |
| 1134 | if (value<0 || value>=st->mode->nbEBands) |
| 1135 | goto bad_arg; |
| 1136 | st->start = value; |
| 1137 | } |
| 1138 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1139 | case CELT_SET_END_BAND_REQUEST: |
| 1140 | { |
| 1141 | celt_int32 value = va_arg(ap, celt_int32); |
| 1142 | if (value<0 || value>=st->mode->nbEBands) |
| 1143 | goto bad_arg; |
| 1144 | st->end = value; |
| 1145 | } |
| 1146 | break; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1147 | case CELT_SET_PREDICTION_REQUEST: |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1148 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1149 | int value = va_arg(ap, celt_int32); |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1150 | if (value<0 || value>2) |
Gregory Maxwell | 98046ca | 2008-12-13 20:42:03 -0500 | [diff] [blame] | 1151 | goto bad_arg; |
| 1152 | if (value==0) |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1153 | { |
| 1154 | st->force_intra = 1; |
Gregory Maxwell | a80958b | 2009-06-29 12:48:57 -0400 | [diff] [blame] | 1155 | } else if (value==1) { |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1156 | st->force_intra = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1157 | } else { |
| 1158 | st->force_intra = 0; |
Gregory Maxwell | 2dd3d32 | 2009-06-05 14:05:51 -0400 | [diff] [blame] | 1159 | } |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1160 | } |
| 1161 | break; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1162 | case CELT_SET_VBR_RATE_REQUEST: |
| 1163 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1164 | celt_int32 value = va_arg(ap, celt_int32); |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 1165 | int frame_rate; |
| 1166 | int N = st->mode->shortMdctSize; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1167 | if (value<0) |
| 1168 | goto bad_arg; |
| 1169 | if (value>3072000) |
| 1170 | value = 3072000; |
Jean-Marc Valin | 8cc945c | 2010-05-29 08:07:18 -0400 | [diff] [blame] | 1171 | frame_rate = ((st->mode->Fs<<3)+(N>>1))/N; |
| 1172 | st->vbr_rate_norm = ((value<<(BITRES+3))+(frame_rate>>1))/frame_rate; |
Gregory Maxwell | 888d8ce | 2009-05-21 04:21:53 -0400 | [diff] [blame] | 1173 | } |
| 1174 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1175 | case CELT_RESET_STATE: |
| 1176 | { |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1177 | CELT_MEMSET((char*)&st->ENCODER_RESET_START, 0, |
| 1178 | celt_encoder_get_size(st->mode, st->channels)- |
| 1179 | ((char*)&st->ENCODER_RESET_START - (char*)st)); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1180 | st->delayedIntra = 1; |
John Ridges | 5378bf8 | 2010-02-12 07:08:01 -0500 | [diff] [blame] | 1181 | st->fold_decision = 1; |
Jean-Marc Valin | 628c025 | 2010-04-16 20:57:56 -0400 | [diff] [blame] | 1182 | st->tonal_average = QCONST16(1.f,8); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1183 | } |
| 1184 | break; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1185 | default: |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1186 | goto bad_request; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1187 | } |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1188 | va_end(ap); |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1189 | return CELT_OK; |
Jean-Marc Valin | b6f9061 | 2008-10-05 22:39:13 -0400 | [diff] [blame] | 1190 | bad_arg: |
| 1191 | va_end(ap); |
| 1192 | return CELT_BAD_ARG; |
| 1193 | bad_request: |
| 1194 | va_end(ap); |
| 1195 | return CELT_UNIMPLEMENTED; |
Jean-Marc Valin | c18fb1d | 2008-09-30 07:36:54 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
Jean-Marc Valin | 56522ad | 2009-06-05 17:17:25 -0400 | [diff] [blame] | 1198 | /**********************************************************************/ |
| 1199 | /* */ |
| 1200 | /* DECODER */ |
| 1201 | /* */ |
| 1202 | /**********************************************************************/ |
Jean-Marc Valin | eafbdd5 | 2009-04-27 19:35:09 -0400 | [diff] [blame] | 1203 | #define DECODE_BUFFER_SIZE 2048 |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1204 | |
Jean-Marc Valin | 276de72 | 2008-02-20 17:45:51 +1100 | [diff] [blame] | 1205 | /** Decoder state |
| 1206 | @brief Decoder state |
| 1207 | */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1208 | struct CELTDecoder { |
| 1209 | const CELTMode *mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1210 | int overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1211 | int channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1212 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1213 | int start, end; |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1214 | |
| 1215 | /* Everything beyond this point gets cleared on a reset */ |
| 1216 | #define DECODER_RESET_START last_pitch_index |
| 1217 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1218 | int last_pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1219 | int loss_count; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1220 | |
| 1221 | celt_sig preemph_memD[2]; |
| 1222 | |
Jean-Marc Valin | ca8b992 | 2010-08-27 16:23:03 -0400 | [diff] [blame] | 1223 | celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */ |
| 1224 | /* celt_word16 lpc[], Size = channels*LPC_ORDER */ |
| 1225 | /* celt_word16 oldEBands[], Size = channels*mode->nbEBands */ |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1226 | }; |
| 1227 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1228 | int celt_decoder_get_size(const CELTMode *mode, int channels) |
| 1229 | { |
| 1230 | int size = sizeof(struct CELTDecoder) |
| 1231 | + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig) |
| 1232 | + channels*LPC_ORDER*sizeof(celt_word16) |
| 1233 | + channels*mode->nbEBands*sizeof(celt_word16); |
| 1234 | return size; |
| 1235 | } |
| 1236 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1237 | CELTDecoder *celt_decoder_create(const CELTMode *mode, int channels, int *error) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1238 | { |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1239 | return celt_decoder_init( |
| 1240 | (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)), |
| 1241 | mode, channels, error); |
| 1242 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1243 | |
Jean-Marc Valin | 7cfb730 | 2010-08-27 16:54:33 -0400 | [diff] [blame] | 1244 | CELTDecoder *celt_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels, int *error) |
| 1245 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1246 | if (channels < 0 || channels > 2) |
| 1247 | { |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1248 | if (error) |
| 1249 | *error = CELT_BAD_ARG; |
| 1250 | return NULL; |
| 1251 | } |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1252 | |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1253 | if (st==NULL) |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1254 | { |
| 1255 | if (error) |
| 1256 | *error = CELT_ALLOC_FAIL; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1257 | return NULL; |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1258 | } |
| 1259 | |
Jean-Marc Valin | 6d3829f | 2010-08-27 17:52:38 -0400 | [diff] [blame] | 1260 | CELT_MEMSET((char*)st, 0, celt_decoder_get_size(mode, channels)); |
| 1261 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1262 | st->mode = mode; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1263 | st->overlap = mode->overlap; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1264 | st->channels = channels; |
Jean-Marc Valin | a5431bf | 2008-01-03 20:53:01 +1100 | [diff] [blame] | 1265 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1266 | st->start = 0; |
Jean-Marc Valin | 8952c45 | 2010-07-16 21:48:44 -0400 | [diff] [blame] | 1267 | st->end = st->mode->effEBands; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1268 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1269 | st->loss_count = 0; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1270 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1271 | if (error) |
| 1272 | *error = CELT_OK; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1273 | return st; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1274 | } |
| 1275 | |
Peter Kirk | 19f9dc9 | 2008-06-06 14:38:38 +0200 | [diff] [blame] | 1276 | void celt_decoder_destroy(CELTDecoder *st) |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1277 | { |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1278 | celt_free(st); |
| 1279 | } |
| 1280 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1281 | 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] | 1282 | { |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1283 | int c; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1284 | int pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1285 | int overlap = st->mode->overlap; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1286 | celt_word16 fade = Q15ONE; |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1287 | int i, len; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1288 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1289 | int offset; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1290 | celt_sig *out_mem[2]; |
| 1291 | celt_sig *decode_mem[2]; |
| 1292 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1293 | celt_word16 *lpc; |
| 1294 | celt_word16 *oldBandE; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1295 | SAVE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1296 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1297 | for (c=0;c<C;c++) |
| 1298 | { |
| 1299 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 1300 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 1301 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
| 1302 | } |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1303 | lpc = (celt_word16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
| 1304 | oldBandE = lpc+C*LPC_ORDER; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1305 | |
Jean-Marc Valin | 24c9cda | 2008-05-02 10:34:07 +1000 | [diff] [blame] | 1306 | len = N+st->mode->overlap; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1307 | |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1308 | if (st->loss_count == 0) |
| 1309 | { |
Jean-Marc Valin | 7a7c42a | 2009-11-25 20:38:52 -0500 | [diff] [blame] | 1310 | celt_word16 pitch_buf[MAX_PERIOD>>1]; |
Jean-Marc Valin | 294863b | 2009-11-08 22:29:54 +0900 | [diff] [blame] | 1311 | celt_word32 tmp=0; |
Jean-Marc Valin | e465c14 | 2009-11-26 00:39:36 -0500 | [diff] [blame] | 1312 | celt_word32 mem0[2]={0,0}; |
| 1313 | celt_word16 mem1[2]={0,0}; |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1314 | int len2 = len; |
| 1315 | /* FIXME: This is a kludge */ |
| 1316 | if (len2>MAX_PERIOD>>1) |
| 1317 | len2 = MAX_PERIOD>>1; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1318 | pitch_downsample(out_mem, pitch_buf, MAX_PERIOD, MAX_PERIOD, |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1319 | C, mem0, mem1); |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1320 | pitch_search(st->mode, pitch_buf+((MAX_PERIOD-len2)>>1), pitch_buf, len2, |
| 1321 | MAX_PERIOD-len2-100, &pitch_index, &tmp, 1<<LM); |
| 1322 | pitch_index = MAX_PERIOD-len2-pitch_index; |
Jean-Marc Valin | 0f0da99 | 2009-08-12 21:34:01 -0400 | [diff] [blame] | 1323 | st->last_pitch_index = pitch_index; |
| 1324 | } else { |
| 1325 | pitch_index = st->last_pitch_index; |
| 1326 | if (st->loss_count < 5) |
| 1327 | fade = QCONST16(.8f,15); |
| 1328 | else |
| 1329 | fade = 0; |
| 1330 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1331 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1332 | for (c=0;c<C;c++) |
| 1333 | { |
Jean-Marc Valin | 6202c74 | 2010-06-01 00:30:37 -0400 | [diff] [blame] | 1334 | /* FIXME: This is more memory than necessary */ |
| 1335 | celt_word32 e[2*MAX_PERIOD]; |
| 1336 | celt_word16 exc[2*MAX_PERIOD]; |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 1337 | celt_word32 ac[LPC_ORDER+1]; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1338 | celt_word16 decay = 1; |
| 1339 | celt_word32 S1=0; |
Jean-Marc Valin | 74128be | 2010-01-01 09:33:17 -0500 | [diff] [blame] | 1340 | celt_word16 mem[LPC_ORDER]={0}; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1341 | |
Jean-Marc Valin | d5f9930 | 2009-12-16 22:42:32 -0500 | [diff] [blame] | 1342 | offset = MAX_PERIOD-pitch_index; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1343 | for (i=0;i<MAX_PERIOD;i++) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1344 | exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1345 | |
| 1346 | if (st->loss_count == 0) |
| 1347 | { |
| 1348 | _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap, |
| 1349 | LPC_ORDER, MAX_PERIOD); |
| 1350 | |
Jean-Marc Valin | 456eab2 | 2010-06-16 22:38:57 -0400 | [diff] [blame] | 1351 | /* Noise floor -40 dB */ |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1352 | #ifdef FIXED_POINT |
| 1353 | ac[0] += SHR32(ac[0],13); |
| 1354 | #else |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1355 | ac[0] *= 1.0001f; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1356 | #endif |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1357 | /* Lag windowing */ |
| 1358 | for (i=1;i<=LPC_ORDER;i++) |
| 1359 | { |
| 1360 | /*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] | 1361 | #ifdef FIXED_POINT |
| 1362 | ac[i] -= MULT16_32_Q15(2*i*i, ac[i]); |
| 1363 | #else |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1364 | ac[i] -= ac[i]*(.008f*i)*(.008f*i); |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1365 | #endif |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1366 | } |
| 1367 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1368 | _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER); |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1369 | } |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1370 | 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] | 1371 | /*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] | 1372 | /* Check if the waveform is decaying (and if so how fast) */ |
| 1373 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1374 | celt_word32 E1=1, E2=1; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1375 | int period; |
| 1376 | if (pitch_index <= MAX_PERIOD/2) |
| 1377 | period = pitch_index; |
| 1378 | else |
| 1379 | period = MAX_PERIOD/2; |
| 1380 | for (i=0;i<period;i++) |
| 1381 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1382 | E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8); |
| 1383 | 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] | 1384 | } |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1385 | if (E1 > E2) |
| 1386 | E1 = E2; |
| 1387 | decay = celt_sqrt(frac_div32(SHR(E1,1),E2)); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* Copy excitation, taking decay into account */ |
| 1391 | for (i=0;i<len+st->mode->overlap;i++) |
| 1392 | { |
| 1393 | if (offset+i >= MAX_PERIOD) |
| 1394 | { |
| 1395 | offset -= pitch_index; |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1396 | decay = MULT16_16_Q15(decay, decay); |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1397 | } |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1398 | 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] | 1399 | 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] | 1400 | } |
| 1401 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1402 | 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] | 1403 | |
| 1404 | { |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1405 | celt_word32 S2=0; |
Jean-Marc Valin | 5a0fae5 | 2009-12-14 21:19:37 -0500 | [diff] [blame] | 1406 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | 7b7f071 | 2010-06-17 20:10:02 -0400 | [diff] [blame] | 1407 | S2 += SHR32(MULT16_16(e[i],e[i]),8); |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1408 | /* This checks for an "explosion" in the synthesis */ |
| 1409 | #ifdef FIXED_POINT |
| 1410 | if (!(S1 > SHR32(S2,2))) |
| 1411 | #else |
| 1412 | /* 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] | 1413 | if (!(S1 > 0.2f*S2)) |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1414 | #endif |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1415 | { |
| 1416 | for (i=0;i<len+overlap;i++) |
| 1417 | e[i] = 0; |
| 1418 | } else if (S1 < S2) |
| 1419 | { |
Jean-Marc Valin | 1299640 | 2010-08-04 09:13:24 -0400 | [diff] [blame] | 1420 | 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] | 1421 | for (i=0;i<len+overlap;i++) |
Jean-Marc Valin | feca095 | 2010-06-17 20:25:51 -0400 | [diff] [blame] | 1422 | e[i] = MULT16_16_Q15(ratio, e[i]); |
Jean-Marc Valin | 07fed1b | 2009-12-28 07:59:42 -0500 | [diff] [blame] | 1423 | } |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1427 | out_mem[c][i] = out_mem[c][N+i]; |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1428 | |
Jean-Marc Valin | e14fe90 | 2009-12-11 00:07:31 -0500 | [diff] [blame] | 1429 | /* Apply TDAC to the concealed audio so that it blends with the |
| 1430 | previous and next frames */ |
| 1431 | for (i=0;i<overlap/2;i++) |
| 1432 | { |
Jean-Marc Valin | d69c1cb | 2009-12-28 00:34:29 -0500 | [diff] [blame] | 1433 | celt_word32 tmp1, tmp2; |
| 1434 | tmp1 = MULT16_32_Q15(st->mode->window[i ], e[i ]) - |
| 1435 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[overlap-i-1]); |
| 1436 | tmp2 = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) + |
| 1437 | MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]); |
| 1438 | tmp1 = MULT16_32_Q15(fade, tmp1); |
| 1439 | tmp2 = MULT16_32_Q15(fade, tmp2); |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1440 | out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp2); |
| 1441 | out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp2); |
| 1442 | out_mem[c][MAX_PERIOD-N+i] += MULT16_32_Q15(st->mode->window[i], tmp1); |
| 1443 | 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] | 1444 | } |
| 1445 | for (i=0;i<N-overlap;i++) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1446 | 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] | 1447 | } |
Jean-Marc Valin | 1677aa9 | 2007-12-08 01:13:34 +1100 | [diff] [blame] | 1448 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1449 | 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] | 1450 | |
| 1451 | st->loss_count++; |
| 1452 | |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1453 | RESTORE_STACK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1454 | } |
| 1455 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1456 | #ifdef FIXED_POINT |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1457 | 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] | 1458 | { |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1459 | #else |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1460 | 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] | 1461 | { |
| 1462 | #endif |
Jean-Marc Valin | 0695a5f | 2010-08-27 11:33:18 -0400 | [diff] [blame] | 1463 | int c, i, N; |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 1464 | int has_fold; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1465 | int bits; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1466 | ec_dec _dec; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 1467 | ec_byte_buffer buf; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1468 | VARDECL(celt_sig, freq); |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1469 | VARDECL(celt_norm, X); |
| 1470 | VARDECL(celt_ener, bandE); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1471 | VARDECL(int, fine_quant); |
| 1472 | VARDECL(int, pulses); |
| 1473 | VARDECL(int, offsets); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1474 | VARDECL(int, fine_priority); |
Jean-Marc Valin | 163b76e | 2010-05-27 23:56:53 -0400 | [diff] [blame] | 1475 | VARDECL(int, tf_res); |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1476 | celt_sig *out_mem[2]; |
| 1477 | celt_sig *decode_mem[2]; |
| 1478 | celt_sig *overlap_mem[2]; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1479 | celt_sig *out_syn[2]; |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1480 | celt_word16 *lpc; |
| 1481 | celt_word16 *oldBandE; |
Jean-Marc Valin | c890b58 | 2008-08-01 22:26:49 -0400 | [diff] [blame] | 1482 | |
Jean-Marc Valin | 9d1decd | 2008-06-17 13:28:13 +1000 | [diff] [blame] | 1483 | int shortBlocks; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1484 | int isTransient; |
Jean-Marc Valin | 05ed03e | 2009-04-29 07:44:13 -0400 | [diff] [blame] | 1485 | int intra_ener; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1486 | int transient_time; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 1487 | int transient_shift; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1488 | int mdct_weight_shift=0; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1489 | const int C = CHANNELS(st->channels); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1490 | int mdct_weight_pos=0; |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1491 | int LM, M; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1492 | int nbFilledBytes, nbAvailableBytes; |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1493 | int effEnd; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 1494 | int codedBands; |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1495 | int alloc_trim; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1496 | SAVE_STACK; |
Jean-Marc Valin | 44ffd5a | 2008-02-22 00:39:25 +1100 | [diff] [blame] | 1497 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1498 | if (pcm==NULL) |
| 1499 | return CELT_BAD_ARG; |
| 1500 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1501 | for (LM=0;LM<4;LM++) |
| 1502 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1503 | break; |
| 1504 | if (LM>=MAX_CONFIG_SIZES) |
| 1505 | return CELT_BAD_ARG; |
| 1506 | M=1<<LM; |
| 1507 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1508 | for (c=0;c<C;c++) |
| 1509 | { |
| 1510 | decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap); |
| 1511 | out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD; |
| 1512 | overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE; |
| 1513 | } |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1514 | lpc = (celt_word16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C); |
| 1515 | oldBandE = lpc+C*LPC_ORDER; |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1516 | |
Jean-Marc Valin | 0475267 | 2010-05-05 07:21:21 -0400 | [diff] [blame] | 1517 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 0141723 | 2008-03-03 13:59:55 +1100 | [diff] [blame] | 1518 | |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1519 | effEnd = st->end; |
| 1520 | if (effEnd > st->mode->effEBands) |
| 1521 | effEnd = st->mode->effEBands; |
| 1522 | |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1523 | ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */ |
| 1524 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
| 1525 | ALLOC(bandE, st->mode->nbEBands*C, celt_ener); |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1526 | for (c=0;c<C;c++) |
| 1527 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1528 | X[c*N+i] = 0; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1529 | for (c=0;c<C;c++) |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1530 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1531 | X[c*N+i] = 0; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1532 | |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1533 | if (data == NULL) |
| 1534 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1535 | celt_decode_lost(st, pcm, N, LM); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1536 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1537 | return CELT_OK; |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1538 | } |
Gregory Maxwell | 520eeae | 2009-02-09 01:33:21 -0500 | [diff] [blame] | 1539 | if (len<0) { |
| 1540 | RESTORE_STACK; |
| 1541 | return CELT_BAD_ARG; |
| 1542 | } |
Jean-Marc Valin | fb83fb4 | 2007-12-08 00:56:04 +1100 | [diff] [blame] | 1543 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1544 | if (dec == NULL) |
| 1545 | { |
| 1546 | ec_byte_readinit(&buf,(unsigned char*)data,len); |
| 1547 | ec_dec_init(&_dec,&buf); |
| 1548 | dec = &_dec; |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1549 | nbFilledBytes = 0; |
| 1550 | } else { |
Jean-Marc Valin | bdcaaf7 | 2010-07-05 13:52:41 -0400 | [diff] [blame] | 1551 | nbFilledBytes = (ec_dec_tell(dec, 0)+4)>>3; |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1552 | } |
Jean-Marc Valin | 6b95d8f | 2010-06-21 21:39:44 -0400 | [diff] [blame] | 1553 | nbAvailableBytes = len-nbFilledBytes; |
| 1554 | |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1555 | /* Decode the global flags (first symbols in the stream) */ |
| 1556 | intra_ener = ec_dec_bit_prob(dec, 8192); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1557 | /* Get band energies */ |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1558 | unquant_coarse_energy(st->mode, st->start, st->end, bandE, oldBandE, |
| 1559 | intra_ener, st->mode->prob, dec, C, LM); |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1560 | |
Jean-Marc Valin | f9fdbff | 2010-09-05 21:02:38 -0400 | [diff] [blame] | 1561 | if (LM > 0) |
| 1562 | isTransient = ec_dec_bit_prob(dec, 8192); |
| 1563 | else |
| 1564 | isTransient = 0; |
Jean-Marc Valin | 017001a | 2010-08-05 15:42:50 -0400 | [diff] [blame] | 1565 | |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1566 | if (isTransient) |
| 1567 | shortBlocks = M; |
| 1568 | else |
| 1569 | shortBlocks = 0; |
| 1570 | |
| 1571 | if (isTransient) |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1572 | { |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1573 | transient_shift = ec_dec_uint(dec, 4); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1574 | if (transient_shift == 3) |
| 1575 | { |
Jean-Marc Valin | 0be05b0 | 2010-07-16 14:23:03 -0400 | [diff] [blame] | 1576 | int transient_time_quant; |
| 1577 | int max_time = (N+st->mode->overlap)*(celt_int32)8000/st->mode->Fs; |
| 1578 | transient_time_quant = ec_dec_uint(dec, max_time); |
| 1579 | transient_time = transient_time_quant*(celt_int32)st->mode->Fs/8000; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1580 | } else { |
| 1581 | mdct_weight_shift = transient_shift; |
Jean-Marc Valin | 3b918ba | 2010-05-05 00:02:26 -0400 | [diff] [blame] | 1582 | if (mdct_weight_shift && M>2) |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1583 | mdct_weight_pos = ec_dec_uint(dec, M-1); |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1584 | transient_shift = 0; |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1585 | transient_time = 0; |
Jean-Marc Valin | aa93625 | 2009-05-29 22:14:20 -0400 | [diff] [blame] | 1586 | } |
Gregory Maxwell | 0527f37 | 2008-09-23 19:28:35 -0400 | [diff] [blame] | 1587 | } else { |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1588 | transient_time = -1; |
Jean-Marc Valin | 528f4b8 | 2008-06-29 03:46:39 +1000 | [diff] [blame] | 1589 | transient_shift = 0; |
Jean-Marc Valin | 12b2248 | 2008-06-16 14:13:05 +1000 | [diff] [blame] | 1590 | } |
Jean-Marc Valin | 0a571ef | 2010-06-05 23:12:19 -0400 | [diff] [blame] | 1591 | |
| 1592 | ALLOC(tf_res, st->mode->nbEBands, int); |
Jean-Marc Valin | 2ed5e67 | 2010-07-13 16:50:11 -0400 | [diff] [blame] | 1593 | 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] | 1594 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1595 | has_fold = ec_dec_bit_prob(dec, 8192)<<1; |
| 1596 | has_fold |= ec_dec_bit_prob(dec, (has_fold>>1) ? 32768 : 49152); |
| 1597 | |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1598 | ALLOC(pulses, st->mode->nbEBands, int); |
| 1599 | ALLOC(offsets, st->mode->nbEBands, int); |
Jean-Marc Valin | 52cb5fb | 2009-06-10 08:08:55 -0400 | [diff] [blame] | 1600 | ALLOC(fine_priority, st->mode->nbEBands, int); |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1601 | |
| 1602 | for (i=0;i<st->mode->nbEBands;i++) |
| 1603 | offsets[i] = 0; |
Jean-Marc Valin | d74c851 | 2010-09-29 17:39:54 -0400 | [diff] [blame] | 1604 | for (i=0;i<st->mode->nbEBands;i++) |
| 1605 | { |
| 1606 | if (ec_dec_bit_prob(dec, 1024)) |
| 1607 | { |
| 1608 | while (ec_dec_bit_prob(dec, 32768)) |
| 1609 | offsets[i]++; |
| 1610 | offsets[i]++; |
| 1611 | offsets[i] *= (6<<BITRES); |
| 1612 | } |
| 1613 | } |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 1614 | |
Jean-Marc Valin | 9099bc3 | 2010-08-07 21:50:01 -0400 | [diff] [blame] | 1615 | ALLOC(fine_quant, st->mode->nbEBands, int); |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 1616 | { |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1617 | int fl; |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 1618 | int trim_index=0; |
| 1619 | fl = ec_decode_bin(dec, 7); |
| 1620 | while (trim_cdf[trim_index+1] <= fl) |
| 1621 | trim_index++; |
| 1622 | ec_dec_update(dec, trim_cdf[trim_index], trim_cdf[trim_index+1], 128); |
| 1623 | alloc_trim = trim_coef[trim_index]; |
Jean-Marc Valin | 6bf0462 | 2010-09-30 10:16:22 -0400 | [diff] [blame] | 1624 | } |
Jean-Marc Valin | f1fea66 | 2010-10-09 22:58:52 -0400 | [diff] [blame] | 1625 | |
| 1626 | bits = len*8 - ec_dec_tell(dec, 0) - 1; |
| 1627 | 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] | 1628 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1629 | 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] | 1630 | |
Jean-Marc Valin | 8cbea17 | 2010-08-05 15:22:57 -0400 | [diff] [blame] | 1631 | /* Decode fixed codebook */ |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 1632 | 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] | 1633 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1634 | unquant_energy_finalise(st->mode, st->start, st->end, bandE, oldBandE, |
| 1635 | 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] | 1636 | |
Jean-Marc Valin | 02a3527 | 2010-08-27 16:00:01 -0400 | [diff] [blame] | 1637 | log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C); |
Jean-Marc Valin | bc272de | 2010-08-02 09:41:31 -0400 | [diff] [blame] | 1638 | |
Jean-Marc Valin | 8861955 | 2009-10-04 21:35:36 -0400 | [diff] [blame] | 1639 | if (mdct_weight_shift) |
| 1640 | { |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1641 | 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] | 1642 | } |
| 1643 | |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1644 | /* Synthesis */ |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1645 | denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M); |
Jean-Marc Valin | a4833ff | 2008-01-10 15:34:00 +1100 | [diff] [blame] | 1646 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1647 | 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] | 1648 | if (C==2) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1649 | 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] | 1650 | |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1651 | for (c=0;c<C;c++) |
| 1652 | for (i=0;i<M*st->mode->eBands[st->start];i++) |
| 1653 | freq[c*N+i] = 0; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1654 | for (c=0;c<C;c++) |
Jean-Marc Valin | 85f41b2 | 2010-07-16 18:12:45 -0400 | [diff] [blame] | 1655 | for (i=M*st->mode->eBands[effEnd];i<N;i++) |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1656 | freq[c*N+i] = 0; |
Jean-Marc Valin | 3a0bc3d | 2010-02-21 15:10:22 -0500 | [diff] [blame] | 1657 | |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1658 | out_syn[0] = out_mem[0]+MAX_PERIOD-N; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1659 | if (C==2) |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1660 | out_syn[1] = out_mem[1]+MAX_PERIOD-N; |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1661 | |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1662 | /* Compute inverse MDCTs */ |
Jean-Marc Valin | e0ce2cf | 2010-08-26 23:58:08 -0400 | [diff] [blame] | 1663 | compute_inv_mdcts(st->mode, shortBlocks, freq, transient_time, |
Jean-Marc Valin | 6d13108 | 2010-08-27 15:15:32 -0400 | [diff] [blame] | 1664 | transient_shift, out_syn, overlap_mem, C, LM); |
Jean-Marc Valin | ffa1347 | 2007-12-10 16:54:17 +1100 | [diff] [blame] | 1665 | |
Jean-Marc Valin | f67b447 | 2010-08-27 01:32:40 -0400 | [diff] [blame] | 1666 | 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] | 1667 | st->loss_count = 0; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 1668 | RESTORE_STACK; |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1669 | if (ec_dec_get_error(dec)) |
| 1670 | return CELT_CORRUPTED_DATA; |
| 1671 | else |
| 1672 | return CELT_OK; |
Jean-Marc Valin | 269d40a | 2007-12-07 11:29:45 +1100 | [diff] [blame] | 1673 | } |
| 1674 | |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1675 | #ifdef FIXED_POINT |
| 1676 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1677 | 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] | 1678 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1679 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1680 | VARDECL(celt_int16, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1681 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1682 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1683 | if (pcm==NULL) |
| 1684 | return CELT_BAD_ARG; |
| 1685 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1686 | for (LM=0;LM<4;LM++) |
| 1687 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1688 | break; |
| 1689 | if (LM>=MAX_CONFIG_SIZES) |
| 1690 | return CELT_BAD_ARG; |
| 1691 | M=1<<LM; |
| 1692 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1693 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1694 | N = M*st->mode->shortMdctSize; |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1695 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 1696 | ALLOC(out, C*N, celt_int16); |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1697 | 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] | 1698 | if (ret==0) |
| 1699 | for (j=0;j<C*N;j++) |
Jean-Marc Valin | ae01e11 | 2010-08-03 21:43:41 -0400 | [diff] [blame] | 1700 | pcm[j]=out[j]*(1.f/32768.f); |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1701 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1702 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1703 | return ret; |
| 1704 | } |
| 1705 | #endif /*DISABLE_FLOAT_API*/ |
| 1706 | #else |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1707 | 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] | 1708 | { |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1709 | int j, ret, C, N, LM, M; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1710 | VARDECL(celt_sig, out); |
Jean-Marc Valin | cb8780c | 2009-07-20 23:40:35 -0400 | [diff] [blame] | 1711 | SAVE_STACK; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1712 | |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1713 | if (pcm==NULL) |
| 1714 | return CELT_BAD_ARG; |
| 1715 | |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1716 | for (LM=0;LM<4;LM++) |
| 1717 | if (st->mode->shortMdctSize<<LM==frame_size) |
| 1718 | break; |
| 1719 | if (LM>=MAX_CONFIG_SIZES) |
| 1720 | return CELT_BAD_ARG; |
| 1721 | M=1<<LM; |
| 1722 | |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 1723 | C = CHANNELS(st->channels); |
Jean-Marc Valin | 017fa85 | 2010-05-06 22:11:48 -0400 | [diff] [blame] | 1724 | N = M*st->mode->shortMdctSize; |
Jean-Marc Valin | 234969c | 2009-10-17 22:12:42 -0400 | [diff] [blame] | 1725 | ALLOC(out, C*N, celt_sig); |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1726 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1727 | 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] | 1728 | |
Jean-Marc Valin | b1e017f | 2010-07-18 21:20:35 -0400 | [diff] [blame] | 1729 | if (ret==0) |
| 1730 | for (j=0;j<C*N;j++) |
| 1731 | pcm[j] = FLOAT2INT16 (out[j]); |
Gregory Maxwell | 0719f6f | 2009-07-09 17:07:24 -0400 | [diff] [blame] | 1732 | |
Wessel Lubberhuizen | 33ba6cc | 2008-10-03 07:09:29 -0400 | [diff] [blame] | 1733 | RESTORE_STACK; |
Gregory Maxwell | 5f2a74b | 2008-08-11 23:50:07 -0400 | [diff] [blame] | 1734 | return ret; |
| 1735 | } |
| 1736 | #endif |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1737 | |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1738 | int celt_decode(CELTDecoder * restrict st, const unsigned char *data, int len, celt_int16 * restrict pcm, int frame_size) |
| 1739 | { |
| 1740 | return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL); |
| 1741 | } |
| 1742 | |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1743 | #ifndef DISABLE_FLOAT_API |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1744 | int celt_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size) |
| 1745 | { |
| 1746 | return celt_decode_with_ec_float(st, data, len, pcm, frame_size, NULL); |
| 1747 | } |
Jean-Marc Valin | 4424b5a | 2010-07-06 19:37:23 -0400 | [diff] [blame] | 1748 | #endif /* DISABLE_FLOAT_API */ |
Jean-Marc Valin | f5e2e32 | 2010-05-19 12:05:02 -0400 | [diff] [blame] | 1749 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1750 | int celt_decoder_ctl(CELTDecoder * restrict st, int request, ...) |
| 1751 | { |
| 1752 | va_list ap; |
Gregory Maxwell | 1716999 | 2009-06-04 15:15:34 -0400 | [diff] [blame] | 1753 | |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1754 | va_start(ap, request); |
| 1755 | switch (request) |
| 1756 | { |
Gregory Maxwell | f3b44ef | 2009-06-03 13:37:45 -0400 | [diff] [blame] | 1757 | case CELT_GET_MODE_REQUEST: |
| 1758 | { |
| 1759 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
| 1760 | if (value==0) |
| 1761 | goto bad_arg; |
| 1762 | *value=st->mode; |
| 1763 | } |
| 1764 | break; |
Jean-Marc Valin | 5f96146 | 2010-05-19 13:38:10 -0400 | [diff] [blame] | 1765 | case CELT_SET_START_BAND_REQUEST: |
| 1766 | { |
| 1767 | celt_int32 value = va_arg(ap, celt_int32); |
| 1768 | if (value<0 || value>=st->mode->nbEBands) |
| 1769 | goto bad_arg; |
| 1770 | st->start = value; |
| 1771 | } |
| 1772 | break; |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 1773 | case CELT_SET_END_BAND_REQUEST: |
| 1774 | { |
| 1775 | celt_int32 value = va_arg(ap, celt_int32); |
| 1776 | if (value<0 || value>=st->mode->nbEBands) |
| 1777 | goto bad_arg; |
| 1778 | st->end = value; |
| 1779 | } |
| 1780 | break; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1781 | case CELT_RESET_STATE: |
| 1782 | { |
Jean-Marc Valin | c09807d | 2010-08-27 17:17:50 -0400 | [diff] [blame] | 1783 | CELT_MEMSET((char*)&st->DECODER_RESET_START, 0, |
| 1784 | celt_decoder_get_size(st->mode, st->channels)- |
| 1785 | ((char*)&st->DECODER_RESET_START - (char*)st)); |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1786 | } |
| 1787 | break; |
| 1788 | default: |
| 1789 | goto bad_request; |
| 1790 | } |
| 1791 | va_end(ap); |
| 1792 | return CELT_OK; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1793 | bad_arg: |
| 1794 | va_end(ap); |
| 1795 | return CELT_BAD_ARG; |
John Ridges | 454d1d0 | 2009-05-21 22:38:39 -0400 | [diff] [blame] | 1796 | bad_request: |
| 1797 | va_end(ap); |
| 1798 | return CELT_UNIMPLEMENTED; |
| 1799 | } |
Jean-Marc Valin | ece94a0 | 2009-10-16 07:30:14 -0400 | [diff] [blame] | 1800 | |
| 1801 | const char *celt_strerror(int error) |
| 1802 | { |
| 1803 | static const char *error_strings[8] = { |
| 1804 | "success", |
| 1805 | "invalid argument", |
| 1806 | "invalid mode", |
| 1807 | "internal error", |
| 1808 | "corrupted stream", |
| 1809 | "request not implemented", |
| 1810 | "invalid state", |
| 1811 | "memory allocation failed" |
| 1812 | }; |
| 1813 | if (error > 0 || error < -7) |
| 1814 | return "unknown error"; |
| 1815 | else |
| 1816 | return error_strings[-error]; |
| 1817 | } |
| 1818 | |