1 /* 2 * Opus decoder/demuxer 3 * Copyright (c) 2012 Andrew D'Addesio 4 * Copyright (c) 2013-2014 Mozilla Corporation 5 * 6 * This file is part of FFmpeg. 7 * 8 * FFmpeg is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * FFmpeg is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with FFmpeg; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 module audioformats.dopus; 23 24 version(decodeOPUS): 25 26 import core.stdc.stdlib : malloc, free, realloc, calloc; 27 import core.stdc.string: memmove, memcpy, memcmp, memset; 28 import core.stdc.math : sqrtf, exp2f, exp2, sqrtf, lrintf; 29 import std.math : cos, sin, PI, floor; 30 31 import audioformats.io; 32 import audioformats.internals; 33 34 35 private: 36 37 nothrow @nogc: 38 39 40 alias FFTSample = float; 41 42 struct FFTComplex { 43 FFTSample re, im; 44 } 45 46 alias int8_t = byte; 47 alias uint8_t = ubyte; 48 alias int16_t = short; 49 alias uint16_t = ushort; 50 alias int32_t = int; 51 alias uint32_t = uint; 52 alias int64_t = long; 53 alias uint64_t = ulong; 54 55 enum AV_NOPTS_VALUE = cast(int64_t)0x8000000000000000UL; 56 57 58 T FFABS(T) (in T a) { return (a < 0 ? -a : a); } 59 60 T FFMAX(T) (in T a, in T b) { return (a > b ? a : b); } 61 T FFMIN(T) (in T a, in T b) { return (a < b ? a : b); } 62 63 T FFMIN3(T) (in T a, in T b, in T c) { return (a < b ? (a < c ? a : c) : (b < c ? b : c)); } 64 65 66 double ff_exp10 (double x) { 67 enum M_LOG2_10 = 3.32192809488736234787; /* log_2 10 */ 68 return exp2(M_LOG2_10 * x); 69 } 70 71 72 static immutable ubyte[256] ff_log2_tab = [ 73 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 74 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 75 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 76 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 77 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 78 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 79 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 80 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 81 ]; 82 83 alias av_log2 = ff_log2; 84 alias ff_log2 = ff_log2_c; 85 86 int ff_log2_c (uint v) nothrow @trusted @nogc { 87 int n = 0; 88 if (v & 0xffff0000) { 89 v >>= 16; 90 n += 16; 91 } 92 if (v & 0xff00) { 93 v >>= 8; 94 n += 8; 95 } 96 n += ff_log2_tab[v]; 97 return n; 98 } 99 100 101 /** 102 * Clear high bits from an unsigned integer starting with specific bit position 103 * @param a value to clip 104 * @param p bit position to clip at 105 * @return clipped value 106 */ 107 uint av_mod_uintp2 (uint a, uint p) pure nothrow @safe @nogc { return a & ((1 << p) - 1); } 108 109 /* a*inverse[b]>>32 == a/b for all 0<=a<=16909558 && 2<=b<=256 110 * for a>16909558, is an overestimate by less than 1 part in 1<<24 */ 111 static immutable uint[257] ff_inverse = [ 112 0, 4294967295U,2147483648U,1431655766, 1073741824, 858993460, 715827883, 613566757, 113 536870912, 477218589, 429496730, 390451573, 357913942, 330382100, 306783379, 286331154, 114 268435456, 252645136, 238609295, 226050911, 214748365, 204522253, 195225787, 186737709, 115 178956971, 171798692, 165191050, 159072863, 153391690, 148102321, 143165577, 138547333, 116 134217728, 130150525, 126322568, 122713352, 119304648, 116080198, 113025456, 110127367, 117 107374183, 104755300, 102261127, 99882961, 97612894, 95443718, 93368855, 91382283, 118 89478486, 87652394, 85899346, 84215046, 82595525, 81037119, 79536432, 78090315, 119 76695845, 75350304, 74051161, 72796056, 71582789, 70409300, 69273667, 68174085, 120 67108864, 66076420, 65075263, 64103990, 63161284, 62245903, 61356676, 60492498, 121 59652324, 58835169, 58040099, 57266231, 56512728, 55778797, 55063684, 54366675, 122 53687092, 53024288, 52377650, 51746594, 51130564, 50529028, 49941481, 49367441, 123 48806447, 48258060, 47721859, 47197443, 46684428, 46182445, 45691142, 45210183, 124 44739243, 44278014, 43826197, 43383509, 42949673, 42524429, 42107523, 41698712, 125 41297763, 40904451, 40518560, 40139882, 39768216, 39403370, 39045158, 38693400, 126 38347923, 38008561, 37675152, 37347542, 37025581, 36709123, 36398028, 36092163, 127 35791395, 35495598, 35204650, 34918434, 34636834, 34359739, 34087043, 33818641, 128 33554432, 33294321, 33038210, 32786010, 32537632, 32292988, 32051995, 31814573, 129 31580642, 31350127, 31122952, 30899046, 30678338, 30460761, 30246249, 30034737, 130 29826162, 29620465, 29417585, 29217465, 29020050, 28825284, 28633116, 28443493, 131 28256364, 28071682, 27889399, 27709467, 27531842, 27356480, 27183338, 27012373, 132 26843546, 26676816, 26512144, 26349493, 26188825, 26030105, 25873297, 25718368, 133 25565282, 25414008, 25264514, 25116768, 24970741, 24826401, 24683721, 24542671, 134 24403224, 24265352, 24129030, 23994231, 23860930, 23729102, 23598722, 23469767, 135 23342214, 23216040, 23091223, 22967740, 22845571, 22724695, 22605092, 22486740, 136 22369622, 22253717, 22139007, 22025474, 21913099, 21801865, 21691755, 21582751, 137 21474837, 21367997, 21262215, 21157475, 21053762, 20951060, 20849356, 20748635, 138 20648882, 20550083, 20452226, 20355296, 20259280, 20164166, 20069941, 19976593, 139 19884108, 19792477, 19701685, 19611723, 19522579, 19434242, 19346700, 19259944, 140 19173962, 19088744, 19004281, 18920561, 18837576, 18755316, 18673771, 18592933, 141 18512791, 18433337, 18354562, 18276457, 18199014, 18122225, 18046082, 17970575, 142 17895698, 17821442, 17747799, 17674763, 17602325, 17530479, 17459217, 17388532, 143 17318417, 17248865, 17179870, 17111424, 17043522, 16976156, 16909321, 16843010, 144 16777216 145 ]; 146 147 148 static immutable ubyte[256] ff_sqrt_tab = [ 149 0, 16, 23, 28, 32, 36, 40, 43, 46, 48, 51, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 77, 79, 80, 82, 84, 85, 87, 88, 90, 150 91, 92, 94, 95, 96, 98, 99,100,102,103,104,105,107,108,109,110,111,112,114,115,116,117,118,119,120,121,122,123,124,125,126,127, 151 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,144,145,146,147,148,149,150,151,151,152,153,154,155,156,156, 152 157,158,159,160,160,161,162,163,164,164,165,166,167,168,168,169,170,171,171,172,173,174,174,175,176,176,177,178,179,179,180,181, 153 182,182,183,184,184,185,186,186,187,188,188,189,190,190,191,192,192,193,194,194,195,196,196,197,198,198,199,200,200,201,202,202, 154 203,204,204,205,205,206,207,207,208,208,209,210,210,211,212,212,213,213,214,215,215,216,216,217,218,218,219,219,220,220,221,222, 155 222,223,223,224,224,225,226,226,227,227,228,228,229,230,230,231,231,232,232,233,233,234,235,235,236,236,237,237,238,238,239,239, 156 240,240,241,242,242,243,243,244,244,245,245,246,246,247,247,248,248,249,249,250,250,251,251,252,252,253,253,254,254,255,255,255 157 ]; 158 159 uint FASTDIV() (uint a, uint b) { return (cast(uint)(((cast(ulong)a) * ff_inverse[b]) >> 32)); } 160 161 uint ff_sqrt (uint a) nothrow @safe @nogc { 162 uint b; 163 alias av_log2_16bit = av_log2; 164 165 if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4; 166 else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2; 167 //#if !CONFIG_SMALL 168 else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1; 169 else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8]; 170 //#endif 171 else { 172 int s = av_log2_16bit(a >> 16) >> 1; 173 uint c = a >> (s + 2); 174 b = ff_sqrt_tab[c >> (s + 8)]; 175 b = FASTDIV(c,b) + (b << s); 176 } 177 return b - (a < b * b); 178 } 179 180 /** 181 * Clip a signed integer value into the amin-amax range. 182 * @param a value to clip 183 * @param amin minimum value of the clip range 184 * @param amax maximum value of the clip range 185 * @return clipped value 186 */ 187 int av_clip (int a, int amin, int amax) pure nothrow @safe @nogc { 188 pragma(inline, true); 189 //if (a < amin) return amin; else if (a > amax) return amax; else return a; 190 return (a < amin ? amin : a > amax ? amax : a); 191 } 192 193 /** 194 * Clip a signed integer to an unsigned power of two range. 195 * @param a value to clip 196 * @param p bit position to clip at 197 * @return clipped value 198 */ 199 uint av_clip_uintp2 (int a, int p) pure nothrow @safe @nogc { 200 pragma(inline, true); 201 //if (a & ~((1<<p) - 1)) return -a >> 31 & ((1<<p) - 1); else return a; 202 return (a & ~((1<<p) - 1) ? -a >> 31 & ((1<<p) - 1) : a); 203 } 204 205 /** 206 * Clip a signed integer value into the -32768,32767 range. 207 * @param a value to clip 208 * @return clipped value 209 */ 210 short av_clip_int16 (int a) pure nothrow @safe @nogc { 211 pragma(inline, true); 212 return cast(short)((a+0x8000U) & ~0xFFFF ? (a>>31) ^ 0x7FFF : a); 213 } 214 215 /** 216 * Clip a float value into the amin-amax range. 217 * @param a value to clip 218 * @param amin minimum value of the clip range 219 * @param amax maximum value of the clip range 220 * @return clipped value 221 */ 222 float av_clipf (float a, float amin, float amax) pure nothrow @safe @nogc { 223 pragma(inline, true); 224 return (a < amin ? amin : a > amax ? amax : a); 225 } 226 227 228 // ////////////////////////////////////////////////////////////////////////// // 229 // dsp part 230 void vector_fmul_window (float* dst, const(float)* src0, const(float)* src1, const(float)* win, int len) { 231 int i, j; 232 dst += len; 233 win += len; 234 src0 += len; 235 for (i = -len, j = len-1; i < 0; ++i, --j) { 236 float s0 = src0[i]; 237 float s1 = src1[j]; 238 float wi = win[i]; 239 float wj = win[j]; 240 dst[i] = s0*wj-s1*wi; 241 dst[j] = s0*wi+s1*wj; 242 } 243 } 244 245 static void vector_fmac_scalar (float* dst, const(float)* src, float mul, int len) { 246 for (int i = 0; i < len; i++) dst[i] += src[i]*mul; 247 } 248 249 static void vector_fmul_scalar (float* dst, const(float)* src, float mul, int len) { 250 for (int i = 0; i < len; ++i) dst[i] = src[i]*mul; 251 } 252 253 254 enum { 255 EOK = 0, 256 EINVAL, 257 ENOMEM, 258 } 259 260 int AVERROR (int v) { return -v; } 261 262 enum AVERROR_INVALIDDATA = -EINVAL; 263 enum AVERROR_PATCHWELCOME = -EINVAL; 264 enum AVERROR_BUG = -EINVAL; 265 266 void av_free(T) (T* p) { 267 if (p !is null) { 268 free(p); 269 } 270 } 271 272 273 void av_freep(T) (T** p) { 274 if (p !is null) { 275 if (*p !is null) { 276 free(*p); 277 *p = null; 278 } 279 } 280 } 281 282 283 T* av_mallocz(T) (size_t cnt=1) { 284 if (cnt == 0) return null; 285 return cast(T*)calloc(cnt, T.sizeof); 286 } 287 288 alias av_malloc_array = av_mallocz; 289 alias av_mallocz_array = av_mallocz; 290 alias av_malloc = av_mallocz; 291 292 293 294 /* 295 * Allocates a buffer, reusing the given one if large enough. 296 * Contrary to av_fast_realloc the current buffer contents might not be preserved and on error 297 * the old buffer is freed, thus no special handling to avoid memleaks is necessary. 298 */ 299 void av_fast_malloc (void** ptr, int* size, uint min_size) { 300 static T FFMAX(T) (in T a, in T b) { return (a > b ? a : b); } 301 void **p = ptr; 302 if (min_size < *size) return; 303 *size= FFMAX(17*min_size/16+32, min_size); 304 av_free(*p); 305 *p = av_malloc!ubyte(*size); 306 if (!*p) *size = 0; 307 } 308 309 310 struct AVAudioFifo { 311 //int fmt; // 8 312 uint chans; 313 float* buf; 314 uint rdpos; 315 uint used; 316 uint alloced; 317 } 318 319 int av_audio_fifo_size (AVAudioFifo* af) { 320 return (af !is null ? (af.used-af.rdpos)/af.chans : -1); 321 } 322 323 int av_audio_fifo_read (AVAudioFifo* af, void** data, int nb_samples) { 324 if (af is null) return -1; 325 auto dp = cast(float**)data; 326 int total; 327 while (nb_samples > 0) { 328 if (af.used-af.rdpos < af.chans) break; 329 foreach (immutable chn; 0..af.chans) *dp[chn]++ = af.buf[af.rdpos++]; 330 ++total; 331 --nb_samples; 332 } 333 return total; 334 } 335 336 int av_audio_fifo_drain (AVAudioFifo* af, int nb_samples) { 337 if (af is null) return -1; 338 while (nb_samples > 0) { 339 if (af.used-af.rdpos < af.chans) break; 340 af.rdpos += af.chans; 341 --nb_samples; 342 } 343 return 0; 344 } 345 346 int av_audio_fifo_write (AVAudioFifo* af, void** data, int nb_samples) 347 { 348 // wtf. Probably never called then 349 assert(0); 350 } 351 352 AVAudioFifo* av_audio_fifo_alloc (int samplefmt, int channels, int nb_samples) { 353 if (samplefmt != 8) assert(0); 354 if (channels < 1 || channels > 255) assert(0); 355 if (nb_samples < 0) nb_samples = 0; 356 if (nb_samples > int.max/32) nb_samples = int.max/32; 357 AVAudioFifo* av = av_mallocz!AVAudioFifo(1); 358 if (av is null) return null; 359 av.chans = channels; 360 av.alloced = channels*nb_samples; 361 av.buf = av_mallocz!float(av.alloced); 362 if (av.buf is null) { 363 av_free(av); 364 return null; 365 } 366 av.rdpos = 0; 367 av.used = 0; 368 return av; 369 } 370 371 int av_audio_fifo_free (AVAudioFifo* af) { 372 if (af !is null) { 373 if (af.buf !is null) av_free(af.buf); 374 *af = AVAudioFifo.init; 375 av_free(af); 376 } 377 return 0; 378 } 379 380 381 struct AudioChannelMap { 382 int file_idx, stream_idx, channel_idx; // input 383 int ofile_idx, ostream_idx; // output 384 } 385 386 387 enum AV_CH_FRONT_LEFT = 0x00000001; 388 enum AV_CH_FRONT_RIGHT = 0x00000002; 389 enum AV_CH_FRONT_CENTER = 0x00000004; 390 enum AV_CH_LOW_FREQUENCY = 0x00000008; 391 enum AV_CH_BACK_LEFT = 0x00000010; 392 enum AV_CH_BACK_RIGHT = 0x00000020; 393 enum AV_CH_FRONT_LEFT_OF_CENTER = 0x00000040; 394 enum AV_CH_FRONT_RIGHT_OF_CENTER = 0x00000080; 395 enum AV_CH_BACK_CENTER = 0x00000100; 396 enum AV_CH_SIDE_LEFT = 0x00000200; 397 enum AV_CH_SIDE_RIGHT = 0x00000400; 398 enum AV_CH_TOP_CENTER = 0x00000800; 399 enum AV_CH_TOP_FRONT_LEFT = 0x00001000; 400 enum AV_CH_TOP_FRONT_CENTER = 0x00002000; 401 enum AV_CH_TOP_FRONT_RIGHT = 0x00004000; 402 enum AV_CH_TOP_BACK_LEFT = 0x00008000; 403 enum AV_CH_TOP_BACK_CENTER = 0x00010000; 404 enum AV_CH_TOP_BACK_RIGHT = 0x00020000; 405 enum AV_CH_STEREO_LEFT = 0x20000000; ///< Stereo downmix. 406 enum AV_CH_STEREO_RIGHT = 0x40000000; ///< See AV_CH_STEREO_LEFT. 407 enum AV_CH_WIDE_LEFT = 0x0000000080000000UL; 408 enum AV_CH_WIDE_RIGHT = 0x0000000100000000UL; 409 enum AV_CH_SURROUND_DIRECT_LEFT = 0x0000000200000000UL; 410 enum AV_CH_SURROUND_DIRECT_RIGHT = 0x0000000400000000UL; 411 enum AV_CH_LOW_FREQUENCY_2 = 0x0000000800000000UL; 412 413 /** Channel mask value used for AVCodecContext.request_channel_layout 414 to indicate that the user requests the channel order of the decoder output 415 to be the native codec channel order. */ 416 enum AV_CH_LAYOUT_NATIVE = 0x8000000000000000UL; 417 418 /** 419 * @} 420 * @defgroup channel_mask_c Audio channel layouts 421 * @{ 422 * */ 423 enum AV_CH_LAYOUT_MONO = (AV_CH_FRONT_CENTER); 424 enum AV_CH_LAYOUT_STEREO = (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT); 425 enum AV_CH_LAYOUT_2POINT1 = (AV_CH_LAYOUT_STEREO|AV_CH_LOW_FREQUENCY); 426 enum AV_CH_LAYOUT_2_1 = (AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER); 427 enum AV_CH_LAYOUT_SURROUND = (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER); 428 enum AV_CH_LAYOUT_3POINT1 = (AV_CH_LAYOUT_SURROUND|AV_CH_LOW_FREQUENCY); 429 enum AV_CH_LAYOUT_4POINT0 = (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_CENTER); 430 enum AV_CH_LAYOUT_4POINT1 = (AV_CH_LAYOUT_4POINT0|AV_CH_LOW_FREQUENCY); 431 enum AV_CH_LAYOUT_2_2 = (AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT); 432 enum AV_CH_LAYOUT_QUAD = (AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT); 433 enum AV_CH_LAYOUT_5POINT0 = (AV_CH_LAYOUT_SURROUND|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT); 434 enum AV_CH_LAYOUT_5POINT1 = (AV_CH_LAYOUT_5POINT0|AV_CH_LOW_FREQUENCY); 435 enum AV_CH_LAYOUT_5POINT0_BACK = (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT); 436 enum AV_CH_LAYOUT_5POINT1_BACK = (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_LOW_FREQUENCY); 437 enum AV_CH_LAYOUT_6POINT0 = (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_CENTER); 438 enum AV_CH_LAYOUT_6POINT0_FRONT = (AV_CH_LAYOUT_2_2|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER); 439 enum AV_CH_LAYOUT_HEXAGONAL = (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_BACK_CENTER); 440 enum AV_CH_LAYOUT_6POINT1 = (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER); 441 enum AV_CH_LAYOUT_6POINT1_BACK = (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_BACK_CENTER); 442 enum AV_CH_LAYOUT_6POINT1_FRONT = (AV_CH_LAYOUT_6POINT0_FRONT|AV_CH_LOW_FREQUENCY); 443 enum AV_CH_LAYOUT_7POINT0 = (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT); 444 enum AV_CH_LAYOUT_7POINT0_FRONT = (AV_CH_LAYOUT_5POINT0|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER); 445 enum AV_CH_LAYOUT_7POINT1 = (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT); 446 enum AV_CH_LAYOUT_7POINT1_WIDE = (AV_CH_LAYOUT_5POINT1|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER); 447 enum AV_CH_LAYOUT_7POINT1_WIDE_BACK = (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER); 448 enum AV_CH_LAYOUT_OCTAGONAL = (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_CENTER|AV_CH_BACK_RIGHT); 449 enum AV_CH_LAYOUT_HEXADECAGONAL = (AV_CH_LAYOUT_OCTAGONAL|AV_CH_WIDE_LEFT|AV_CH_WIDE_RIGHT|AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT|AV_CH_TOP_BACK_CENTER|AV_CH_TOP_FRONT_CENTER|AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT); 450 enum AV_CH_LAYOUT_STEREO_DOWNMIX = (AV_CH_STEREO_LEFT|AV_CH_STEREO_RIGHT); 451 452 453 struct AVFrame { 454 /** 455 * number of audio samples (per channel) described by this frame 456 */ 457 int nb_samples; 458 /** 459 * For video, size in bytes of each picture line. 460 * For audio, size in bytes of each plane. 461 * 462 * For audio, only linesize[0] may be set. For planar audio, each channel 463 * plane must be the same size. 464 * 465 * For video the linesizes should be multiples of the CPUs alignment 466 * preference, this is 16 or 32 for modern desktop CPUs. 467 * Some code requires such alignment other code can be slower without 468 * correct alignment, for yet other it makes no difference. 469 * 470 * @note The linesize may be larger than the size of usable data -- there 471 * may be extra padding present for performance reasons. 472 */ 473 int[1/*AV_NUM_DATA_POINTERS*/] linesize; 474 /** 475 * pointers to the data planes/channels. 476 * 477 * For video, this should simply point to data[]. 478 * 479 * For planar audio, each channel has a separate data pointer, and 480 * linesize[0] contains the size of each channel buffer. 481 * For packed audio, there is just one data pointer, and linesize[0] 482 * contains the total size of the buffer for all channels. 483 * 484 * Note: Both data and extended_data should always be set in a valid frame, 485 * but for planar audio with more channels that can fit in data, 486 * extended_data must be used in order to access all channels. 487 */ 488 ubyte** extended_data; 489 490 AudioChannelMap* audio_channel_maps; /* one info entry per -map_channel */ 491 int nb_audio_channel_maps; /* number of (valid) -map_channel settings */ 492 } 493 494 495 int ff_get_buffer (AVFrame* frame, int flags) { 496 return 0; 497 } 498 499 500 struct AVCtx { 501 int sample_fmt; 502 int sample_rate; 503 int channels; 504 ubyte* extradata; 505 uint extradata_size; 506 int delay; 507 ulong channel_layout; 508 //void* priv; 509 int preskip; 510 // oggopus_private 511 int need_comments; 512 int64_t cur_dts; 513 } 514 515 516 ushort AV_RL16 (const(void*) b) { 517 version(LittleEndian) { 518 return *cast(const(ushort)*)b; 519 } else { 520 static assert(0, "boo!"); 521 } 522 } 523 524 525 struct AVPacket { 526 /** 527 * A reference to the reference-counted buffer where the packet data is 528 * stored. 529 * May be NULL, then the packet data is not reference-counted. 530 */ 531 //AVBufferRef *buf; 532 /** 533 * Presentation timestamp in AVStream.time_base units; the time at which 534 * the decompressed packet will be presented to the user. 535 * Can be AV_NOPTS_VALUE if it is not stored in the file. 536 * pts MUST be larger or equal to dts as presentation cannot happen before 537 * decompression, unless one wants to view hex dumps. Some formats misuse 538 * the terms dts and pts/cts to mean something different. Such timestamps 539 * must be converted to true pts/dts before they are stored in AVPacket. 540 */ 541 long pts; 542 /** 543 * Decompression timestamp in AVStream.time_base units; the time at which 544 * the packet is decompressed. 545 * Can be AV_NOPTS_VALUE if it is not stored in the file. 546 */ 547 long dts; 548 ubyte *data; 549 int size; 550 int stream_index; 551 /** 552 * A combination of AV_PKT_FLAG values 553 */ 554 int flags; 555 /** 556 * Additional packet data that can be provided by the container. 557 * Packet can contain several types of side information. 558 */ 559 //AVPacketSideData *side_data; 560 int side_data_elems; 561 562 /** 563 * Duration of this packet in AVStream.time_base units, 0 if unknown. 564 * Equals next_pts - this_pts in presentation order. 565 */ 566 long duration; 567 568 long pos; ///< byte position in stream, -1 if unknown 569 } 570 571 struct GetBitContext { 572 nothrow @nogc: 573 private: 574 const(ubyte)* buffer; 575 uint pos; 576 uint bytestotal; 577 ubyte curv; 578 ubyte bleft; 579 580 public: 581 int init_get_bits8 (const(void)* buf, uint bytelen) nothrow @trusted @nogc { 582 if (bytelen >= int.max/16) assert(0, "too big"); 583 buffer = cast(const(ubyte)*)buf; 584 bytestotal = bytelen; 585 bleft = 0; 586 pos = 0; 587 return 0; 588 } 589 590 T get_bits(T=uint) (uint n) @trusted if (__traits(isIntegral, T)) { 591 if (n == 0 || n > 8) assert(0, "invalid number of bits requested"); 592 T res = 0; 593 foreach_reverse (immutable shift; 0..n) { 594 if (bleft == 0) { 595 if (pos < bytestotal) { 596 curv = buffer[pos++]; 597 } else { 598 curv = 0; 599 //throw eobserr; 600 } 601 bleft = 8; 602 } 603 if (curv&0x80) res |= (1U<<shift); 604 curv <<= 1; 605 --bleft; 606 } 607 return res; 608 } 609 } 610 611 612 static immutable uint64_t[9] ff_vorbis_channel_layouts = [ 613 AV_CH_LAYOUT_MONO, 614 AV_CH_LAYOUT_STEREO, 615 2/*AV_CH_LAYOUT_SURROUND*/, 616 3/*AV_CH_LAYOUT_QUAD*/, 617 4/*AV_CH_LAYOUT_5POINT0_BACK*/, 618 5/*AV_CH_LAYOUT_5POINT1_BACK*/, 619 6/*AV_CH_LAYOUT_5POINT1|AV_CH_BACK_CENTER*/, 620 7/*AV_CH_LAYOUT_7POINT1*/, 621 0 622 ]; 623 624 static immutable uint8_t[8][8] ff_vorbis_channel_layout_offsets = [ 625 [ 0 ], 626 [ 0, 1 ], 627 [ 0, 2, 1 ], 628 [ 0, 1, 2, 3 ], 629 [ 0, 2, 1, 3, 4 ], 630 [ 0, 2, 1, 5, 3, 4 ], 631 [ 0, 2, 1, 6, 5, 3, 4 ], 632 [ 0, 2, 1, 7, 5, 6, 3, 4 ], 633 ]; 634 635 636 enum M_SQRT1_2 = 0.70710678118654752440; /* 1/sqrt(2) */ 637 enum M_SQRT2 = 1.41421356237309504880; /* sqrt(2) */ 638 639 640 enum MAX_FRAME_SIZE = 1275; 641 enum MAX_FRAMES = 48; 642 enum MAX_PACKET_DUR = 5760; 643 644 enum CELT_SHORT_BLOCKSIZE = 120; 645 enum CELT_OVERLAP = CELT_SHORT_BLOCKSIZE; 646 enum CELT_MAX_LOG_BLOCKS = 3; 647 enum CELT_MAX_FRAME_SIZE = (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS)); 648 enum CELT_MAX_BANDS = 21; 649 enum CELT_VECTORS = 11; 650 enum CELT_ALLOC_STEPS = 6; 651 enum CELT_FINE_OFFSET = 21; 652 enum CELT_MAX_FINE_BITS = 8; 653 enum CELT_NORM_SCALE = 16384; 654 enum CELT_QTHETA_OFFSET = 4; 655 enum CELT_QTHETA_OFFSET_TWOPHASE = 16; 656 enum CELT_DEEMPH_COEFF = 0.85000610f; 657 enum CELT_POSTFILTER_MINPERIOD = 15; 658 enum CELT_ENERGY_SILENCE = (-28.0f); 659 660 enum SILK_HISTORY = 322; 661 enum SILK_MAX_LPC = 16; 662 663 /* signed 16x16 . 32 multiply */ 664 int MUL16() (int ra, int rb) { return ra*rb; } 665 long MUL64(T0, T1) (T0 a, T1 b) { return cast(int64_t)a * cast(int64_t)b; } 666 long ROUND_MULL() (int a, int b, int s) { return (((MUL64(a, b) >> ((s) - 1)) + 1) >> 1); } 667 int ROUND_MUL16() (int a, int b) { return ((MUL16(a, b) + 16384) >> 15); } 668 669 int opus_ilog (uint i) nothrow @trusted @nogc { return av_log2(i)+!!i; } 670 671 int MULH() (int a, int b) { return cast(int)(MUL64(a, b) >> 32); } 672 long MULL(T0, T1, T2) (T0 a, T1 b, T2 s) { return (MUL64(a, b) >> (s)); } 673 674 675 enum OPUS_TS_HEADER = 0x7FE0; // 0x3ff (11 bits) 676 enum OPUS_TS_MASK = 0xFFE0; // top 11 bits 677 678 static immutable uint8_t[38] opus_default_extradata = [ 679 'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', 680 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 681 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 682 ]; 683 684 alias OpusMode = int; 685 enum /*OpusMode*/:int { 686 OPUS_MODE_SILK, 687 OPUS_MODE_HYBRID, 688 OPUS_MODE_CELT 689 } 690 691 alias OpusBandwidth = int; 692 enum /*OpusBandwidth*/: int { 693 OPUS_BANDWIDTH_NARROWBAND, 694 OPUS_BANDWIDTH_MEDIUMBAND, 695 OPUS_BANDWIDTH_WIDEBAND, 696 OPUS_BANDWIDTH_SUPERWIDEBAND, 697 OPUS_BANDWIDTH_FULLBAND 698 }; 699 700 struct RawBitsContext { 701 const(uint8_t)* position; 702 uint bytes; 703 uint cachelen; 704 uint cacheval; 705 } 706 707 struct OpusRangeCoder { 708 GetBitContext gb; 709 RawBitsContext rb; 710 uint range; 711 uint value; 712 uint total_read_bits; 713 } 714 715 struct OpusPacket { 716 int packet_size; /**< packet size */ 717 int data_size; /**< size of the useful data -- packet size - padding */ 718 int code; /**< packet code: specifies the frame layout */ 719 int stereo; /**< whether this packet is mono or stereo */ 720 int vbr; /**< vbr flag */ 721 int config; /**< configuration: tells the audio mode, 722 ** bandwidth, and frame duration */ 723 int frame_count; /**< frame count */ 724 int[MAX_FRAMES] frame_offset; /**< frame offsets */ 725 int[MAX_FRAMES] frame_size; /**< frame sizes */ 726 int frame_duration; /**< frame duration, in samples @ 48kHz */ 727 OpusMode mode; /**< mode */ 728 OpusBandwidth bandwidth; /**< bandwidth */ 729 } 730 731 struct OpusStreamContext { 732 //AVCodecContext *avctx; 733 //AVCtx* avctx; 734 int output_channels; 735 736 OpusRangeCoder rc; 737 OpusRangeCoder redundancy_rc; 738 SilkContext *silk; 739 CeltContext *celt; 740 //AVFloatDSPContext *fdsp; 741 742 float[960][2] silk_buf; 743 float*[2] silk_output; 744 //DECLARE_ALIGNED(32, float, celt_buf)[2][960]; 745 float[960][2] celt_buf; 746 float*[2] celt_output; 747 748 float[960][2] redundancy_buf; 749 float*[2] redundancy_output; 750 751 /* data buffers for the final output data */ 752 float*[2] out_; 753 int out_size; 754 755 float *out_dummy; 756 int out_dummy_allocated_size; 757 758 //SwrContext *swr; 759 OpusResampler flr; 760 AVAudioFifo *celt_delay; 761 int silk_samplerate; 762 /* number of samples we still want to get from the resampler */ 763 int delayed_samples; 764 765 OpusPacket packet; 766 767 int redundancy_idx; 768 } 769 770 // a mapping between an opus stream and an output channel 771 struct ChannelMap { 772 int stream_idx; 773 int channel_idx; 774 775 // when a single decoded channel is mapped to multiple output channels, we 776 // write to the first output directly and copy from it to the others 777 // this field is set to 1 for those copied output channels 778 int copy; 779 // this is the index of the output channel to copy from 780 int copy_idx; 781 782 // this channel is silent 783 int silence; 784 } 785 786 struct OpusContext { 787 OpusStreamContext *streams; 788 789 int in_channels; 790 791 /* current output buffers for each streams */ 792 float **out_; 793 int *out_size; 794 /* Buffers for synchronizing the streams when they have different resampling delays */ 795 AVAudioFifo **sync_buffers; 796 /* number of decoded samples for each stream */ 797 int *decoded_samples; 798 799 int nb_streams; 800 int nb_stereo_streams; 801 802 //AVFloatDSPContext *fdsp; 803 int16_t gain_i; 804 float gain; 805 806 ChannelMap *channel_maps; 807 } 808 809 /*static av_always_inline*/ void opus_rc_normalize(OpusRangeCoder *rc) 810 { 811 while (rc.range <= 1<<23) { 812 ubyte b = cast(ubyte)rc.gb.get_bits(8)^0xFF; 813 //conwritefln!"b=0x%02x"(b); 814 //rc.value = ((rc.value << 8) | (rc.gb.get_bits(8) ^ 0xFF)) & ((1u << 31) - 1); 815 rc.value = ((rc.value << 8) | b) & ((1u << 31) - 1); 816 rc.range <<= 8; 817 rc.total_read_bits += 8; 818 } 819 820 /+ 821 /*If the range is too small, rescale it and input some bits.*/ 822 while(_this->rng<=EC_CODE_BOT){ 823 int sym; 824 _this->nbits_total+=EC_SYM_BITS; 825 _this->rng<<=EC_SYM_BITS; 826 /*Use up the remaining bits from our last symbol.*/ 827 sym=_this->rem; 828 /*Read the next value from the input.*/ 829 _this->rem=ec_read_byte(_this); 830 /*Take the rest of the bits we need from this new symbol.*/ 831 sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA); 832 833 sym=(sym<<8|_this->rem)>>1; 834 835 /*And subtract them from val, capped to be less than EC_CODE_TOP.*/ 836 _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1); 837 } 838 +/ 839 } 840 841 /*static av_always_inline*/ void opus_rc_update(OpusRangeCoder *rc, uint scale, 842 uint low, uint high, 843 uint total) 844 { 845 rc.value -= scale * (total - high); 846 rc.range = low ? scale * (high - low) 847 : rc.range - scale * (total - high); 848 opus_rc_normalize(rc); 849 } 850 851 /*static av_always_inline*/ uint opus_rc_getsymbol(OpusRangeCoder *rc, const(uint16_t)*cdf) 852 { 853 uint k, scale, total, symbol, low, high; 854 855 total = *cdf++; 856 857 scale = rc.range / total; 858 symbol = rc.value / scale + 1; 859 symbol = total - FFMIN(symbol, total); 860 861 for (k = 0; cdf[k] <= symbol; k++) {} 862 high = cdf[k]; 863 low = k ? cdf[k-1] : 0; 864 865 opus_rc_update(rc, scale, low, high, total); 866 867 return k; 868 } 869 870 /*static av_always_inline*/ uint opus_rc_p2model(OpusRangeCoder *rc, uint bits) 871 { 872 uint k, scale; 873 scale = rc.range >> bits; // in this case, scale = symbol 874 875 if (rc.value >= scale) { 876 rc.value -= scale; 877 rc.range -= scale; 878 k = 0; 879 } else { 880 rc.range = scale; 881 k = 1; 882 } 883 opus_rc_normalize(rc); 884 return k; 885 } 886 887 /** 888 * CELT: estimate bits of entropy that have thus far been consumed for the 889 * current CELT frame, to integer and fractional (1/8th bit) precision 890 */ 891 /*static av_always_inline*/ uint opus_rc_tell(const OpusRangeCoder *rc) 892 { 893 return rc.total_read_bits - av_log2(rc.range) - 1; 894 } 895 896 /*static av_always_inline*/ uint opus_rc_tell_frac(const OpusRangeCoder *rc) 897 { 898 uint i, total_bits, rcbuffer, range; 899 900 total_bits = rc.total_read_bits << 3; 901 rcbuffer = av_log2(rc.range) + 1; 902 range = rc.range >> (rcbuffer-16); 903 904 for (i = 0; i < 3; i++) { 905 int bit; 906 range = range * range >> 15; 907 bit = range >> 16; 908 rcbuffer = rcbuffer << 1 | bit; 909 range >>= bit; 910 } 911 912 return total_bits - rcbuffer; 913 } 914 915 /** 916 * CELT: read 1-25 raw bits at the end of the frame, backwards byte-wise 917 */ 918 /*static av_always_inline*/ uint opus_getrawbits(OpusRangeCoder *rc, uint count) 919 { 920 uint value = 0; 921 922 while (rc.rb.bytes && rc.rb.cachelen < count) { 923 rc.rb.cacheval |= *--rc.rb.position << rc.rb.cachelen; 924 rc.rb.cachelen += 8; 925 rc.rb.bytes--; 926 } 927 928 value = av_mod_uintp2(rc.rb.cacheval, count); 929 rc.rb.cacheval >>= count; 930 rc.rb.cachelen -= count; 931 rc.total_read_bits += count; 932 933 return value; 934 } 935 936 /** 937 * CELT: read a uniform distribution 938 */ 939 /*static av_always_inline*/ uint opus_rc_unimodel(OpusRangeCoder *rc, uint size) 940 { 941 uint bits, k, scale, total; 942 943 bits = opus_ilog(size - 1); 944 total = (bits > 8) ? ((size - 1) >> (bits - 8)) + 1 : size; 945 946 scale = rc.range / total; 947 k = rc.value / scale + 1; 948 k = total - FFMIN(k, total); 949 opus_rc_update(rc, scale, k, k + 1, total); 950 951 if (bits > 8) { 952 k = k << (bits - 8) | opus_getrawbits(rc, bits - 8); 953 return FFMIN(k, size - 1); 954 } else 955 return k; 956 } 957 958 /*static av_always_inline*/ int opus_rc_laplace(OpusRangeCoder *rc, uint symbol, int decay) 959 { 960 /* extends the range coder to model a Laplace distribution */ 961 int value = 0; 962 uint scale, low = 0, center; 963 964 scale = rc.range >> 15; 965 center = rc.value / scale + 1; 966 center = (1 << 15) - FFMIN(center, 1 << 15); 967 968 if (center >= symbol) { 969 value++; 970 low = symbol; 971 symbol = 1 + ((32768 - 32 - symbol) * (16384-decay) >> 15); 972 973 while (symbol > 1 && center >= low + 2 * symbol) { 974 value++; 975 symbol *= 2; 976 low += symbol; 977 symbol = (((symbol - 2) * decay) >> 15) + 1; 978 } 979 980 if (symbol <= 1) { 981 int distance = (center - low) >> 1; 982 value += distance; 983 low += 2 * distance; 984 } 985 986 if (center < low + symbol) 987 value *= -1; 988 else 989 low += symbol; 990 } 991 992 opus_rc_update(rc, scale, low, FFMIN(low + symbol, 32768), 32768); 993 994 return value; 995 } 996 997 /*static av_always_inline*/ uint opus_rc_stepmodel(OpusRangeCoder *rc, int k0) 998 { 999 /* Use a probability of 3 up to itheta=8192 and then use 1 after */ 1000 uint k, scale, symbol, total = (k0+1)*3 + k0; 1001 scale = rc.range / total; 1002 symbol = rc.value / scale + 1; 1003 symbol = total - FFMIN(symbol, total); 1004 1005 k = (symbol < (k0+1)*3) ? symbol/3 : symbol - (k0+1)*2; 1006 1007 opus_rc_update(rc, scale, (k <= k0) ? 3*(k+0) : (k-1-k0) + 3*(k0+1), 1008 (k <= k0) ? 3*(k+1) : (k-0-k0) + 3*(k0+1), total); 1009 return k; 1010 } 1011 1012 /*static av_always_inline*/ uint opus_rc_trimodel(OpusRangeCoder *rc, int qn) 1013 { 1014 uint k, scale, symbol, total, low, center; 1015 1016 total = ((qn>>1) + 1) * ((qn>>1) + 1); 1017 scale = rc.range / total; 1018 center = rc.value / scale + 1; 1019 center = total - FFMIN(center, total); 1020 1021 if (center < total >> 1) { 1022 k = (ff_sqrt(8 * center + 1) - 1) >> 1; 1023 low = k * (k + 1) >> 1; 1024 symbol = k + 1; 1025 } else { 1026 k = (2*(qn + 1) - ff_sqrt(8*(total - center - 1) + 1)) >> 1; 1027 low = total - ((qn + 1 - k) * (qn + 2 - k) >> 1); 1028 symbol = qn + 1 - k; 1029 } 1030 1031 opus_rc_update(rc, scale, low, low + symbol, total); 1032 1033 return k; 1034 } 1035 1036 1037 static immutable uint16_t[32] opus_frame_duration = [ 1038 480, 960, 1920, 2880, 1039 480, 960, 1920, 2880, 1040 480, 960, 1920, 2880, 1041 480, 960, 1042 480, 960, 1043 120, 240, 480, 960, 1044 120, 240, 480, 960, 1045 120, 240, 480, 960, 1046 120, 240, 480, 960, 1047 ]; 1048 1049 /** 1050 * Read a 1- or 2-byte frame length 1051 */ 1052 int xiph_lacing_16bit (const(uint8_t)** ptr, const(uint8_t)* end) { 1053 int val; 1054 if (*ptr >= end) return AVERROR_INVALIDDATA; 1055 val = *(*ptr)++; 1056 if (val >= 252) { 1057 if (*ptr >= end) return AVERROR_INVALIDDATA; 1058 val += 4 * *(*ptr)++; 1059 } 1060 return val; 1061 } 1062 1063 /** 1064 * Read a multi-byte length (used for code 3 packet padding size) 1065 */ 1066 int xiph_lacing_full (const(uint8_t)** ptr, const(uint8_t)* end) { 1067 int val = 0; 1068 int next; 1069 for (;;) { 1070 if (*ptr >= end || val > int.max-254) return AVERROR_INVALIDDATA; 1071 next = *(*ptr)++; 1072 val += next; 1073 if (next < 255) break; else --val; 1074 } 1075 return val; 1076 } 1077 1078 /** 1079 * Parse Opus packet info from raw packet data 1080 */ 1081 int ff_opus_parse_packet (OpusPacket* pkt, const(uint8_t)* buf, int buf_size, bool self_delimiting) { 1082 1083 const(uint8_t)* ptr = buf; 1084 const(uint8_t)* end = buf+buf_size; 1085 int padding = 0; 1086 int frame_bytes, i; 1087 //conwriteln("frame packet size=", buf_size); 1088 1089 if (buf_size < 1) goto fail; 1090 1091 // TOC byte 1092 i = *ptr++; 1093 pkt.code = (i )&0x3; 1094 pkt.stereo = (i>>2)&0x1; 1095 pkt.config = (i>>3)&0x1F; 1096 1097 // code 2 and code 3 packets have at least 1 byte after the TOC 1098 if (pkt.code >= 2 && buf_size < 2) goto fail; 1099 1100 //conwriteln("packet code: ", pkt.code); 1101 final switch (pkt.code) { 1102 case 0: 1103 // 1 frame 1104 pkt.frame_count = 1; 1105 pkt.vbr = 0; 1106 1107 if (self_delimiting) { 1108 int len = xiph_lacing_16bit(&ptr, end); 1109 if (len < 0 || len > end-ptr) goto fail; 1110 end = ptr+len; 1111 buf_size = cast(int)(end-buf); 1112 } 1113 1114 frame_bytes = cast(int)(end-ptr); 1115 if (frame_bytes > MAX_FRAME_SIZE) goto fail; 1116 pkt.frame_offset[0] = cast(int)(ptr-buf); 1117 pkt.frame_size[0] = frame_bytes; 1118 break; 1119 case 1: 1120 // 2 frames, equal size 1121 pkt.frame_count = 2; 1122 pkt.vbr = 0; 1123 1124 if (self_delimiting) { 1125 int len = xiph_lacing_16bit(&ptr, end); 1126 if (len < 0 || 2 * len > end-ptr) goto fail; 1127 end = ptr+2*len; 1128 buf_size = cast(int)(end-buf); 1129 } 1130 1131 frame_bytes = cast(int)(end-ptr); 1132 if ((frame_bytes&1) != 0 || (frame_bytes>>1) > MAX_FRAME_SIZE) goto fail; 1133 pkt.frame_offset[0] = cast(int)(ptr-buf); 1134 pkt.frame_size[0] = frame_bytes>>1; 1135 pkt.frame_offset[1] = pkt.frame_offset[0]+pkt.frame_size[0]; 1136 pkt.frame_size[1] = frame_bytes>>1; 1137 break; 1138 case 2: 1139 // 2 frames, different sizes 1140 pkt.frame_count = 2; 1141 pkt.vbr = 1; 1142 1143 // read 1st frame size 1144 frame_bytes = xiph_lacing_16bit(&ptr, end); 1145 if (frame_bytes < 0) goto fail; 1146 1147 if (self_delimiting) { 1148 int len = xiph_lacing_16bit(&ptr, end); 1149 if (len < 0 || len+frame_bytes > end-ptr) goto fail; 1150 end = ptr+frame_bytes+len; 1151 buf_size = cast(int)(end-buf); 1152 } 1153 1154 pkt.frame_offset[0] = cast(int)(ptr-buf); 1155 pkt.frame_size[0] = frame_bytes; 1156 1157 // calculate 2nd frame size 1158 frame_bytes = cast(int)(end-ptr-pkt.frame_size[0]); 1159 if (frame_bytes < 0 || frame_bytes > MAX_FRAME_SIZE) goto fail; 1160 pkt.frame_offset[1] = pkt.frame_offset[0]+pkt.frame_size[0]; 1161 pkt.frame_size[1] = frame_bytes; 1162 break; 1163 case 3: 1164 // 1 to 48 frames, can be different sizes 1165 i = *ptr++; 1166 pkt.frame_count = (i )&0x3F; 1167 padding = (i>>6)&0x01; 1168 pkt.vbr = (i>>7)&0x01; 1169 //conwriteln(" frc=", pkt.frame_count, "; padding=", padding, "; vbr=", pkt.vbr); 1170 1171 if (pkt.frame_count == 0 || pkt.frame_count > MAX_FRAMES) goto fail; 1172 1173 // read padding size 1174 if (padding) { 1175 padding = xiph_lacing_full(&ptr, end); 1176 if (padding < 0) goto fail; 1177 //conwriteln(" real padding=", padding); 1178 } 1179 1180 // read frame sizes 1181 if (pkt.vbr) { 1182 // for VBR, all frames except the final one have their size coded in the bitstream. the last frame size is implicit 1183 int total_bytes = 0; 1184 for (i = 0; i < pkt.frame_count-1; i++) { 1185 frame_bytes = xiph_lacing_16bit(&ptr, end); 1186 if (frame_bytes < 0) goto fail; 1187 pkt.frame_size[i] = frame_bytes; 1188 total_bytes += frame_bytes; 1189 } 1190 1191 if (self_delimiting) { 1192 int len = xiph_lacing_16bit(&ptr, end); 1193 if (len < 0 || len+total_bytes+padding > end-ptr) goto fail; 1194 end = ptr+total_bytes+len+padding; 1195 buf_size = cast(int)(end-buf); 1196 } 1197 1198 frame_bytes = cast(int)(end-ptr-padding); 1199 if (total_bytes > frame_bytes) goto fail; 1200 pkt.frame_offset[0] = cast(int)(ptr-buf); 1201 for (i = 1; i < pkt.frame_count; i++) pkt.frame_offset[i] = pkt.frame_offset[i-1]+pkt.frame_size[i-1]; 1202 pkt.frame_size[pkt.frame_count-1] = frame_bytes-total_bytes; 1203 } else { 1204 // for CBR, the remaining packet bytes are divided evenly between the frames 1205 if (self_delimiting) { 1206 frame_bytes = xiph_lacing_16bit(&ptr, end); 1207 //conwriteln("frame_bytes=", frame_bytes); 1208 if (frame_bytes < 0 || pkt.frame_count*frame_bytes+padding > end-ptr) goto fail; 1209 end = ptr+pkt.frame_count*frame_bytes+padding; 1210 buf_size = cast(int)(end-buf); 1211 } else { 1212 frame_bytes = cast(int)(end-ptr-padding); 1213 //conwriteln("frame_bytes=", frame_bytes); 1214 if (frame_bytes % pkt.frame_count || frame_bytes/pkt.frame_count > MAX_FRAME_SIZE) goto fail; 1215 frame_bytes /= pkt.frame_count; 1216 } 1217 1218 pkt.frame_offset[0] = cast(int)(ptr-buf); 1219 pkt.frame_size[0] = frame_bytes; 1220 for (i = 1; i < pkt.frame_count; i++) { 1221 pkt.frame_offset[i] = pkt.frame_offset[i-1]+pkt.frame_size[i-1]; 1222 pkt.frame_size[i] = frame_bytes; 1223 } 1224 } 1225 break; 1226 } 1227 1228 pkt.packet_size = buf_size; 1229 pkt.data_size = pkt.packet_size-padding; 1230 1231 // total packet duration cannot be larger than 120ms 1232 pkt.frame_duration = opus_frame_duration[pkt.config]; 1233 if (pkt.frame_duration*pkt.frame_count > MAX_PACKET_DUR) goto fail; 1234 1235 // set mode and bandwidth 1236 if (pkt.config < 12) { 1237 pkt.mode = OPUS_MODE_SILK; 1238 pkt.bandwidth = pkt.config>>2; 1239 //conwriteln("SILK: ", pkt.bandwidth); 1240 } else if (pkt.config < 16) { 1241 pkt.mode = OPUS_MODE_HYBRID; 1242 pkt.bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND+(pkt.config >= 14 ? 1 : 0); 1243 //conwriteln("HYB: ", pkt.bandwidth); 1244 } else { 1245 pkt.mode = OPUS_MODE_CELT; 1246 pkt.bandwidth = (pkt.config-16)>>2; 1247 // skip medium band 1248 if (pkt.bandwidth) ++pkt.bandwidth; 1249 //conwriteln("CELT: ", pkt.bandwidth); 1250 } 1251 1252 return 0; 1253 1254 fail: 1255 memset(pkt, 0, (*pkt).sizeof); 1256 return AVERROR_INVALIDDATA; 1257 } 1258 1259 static int channel_reorder_vorbis(int nb_channels, int channel_idx) 1260 { 1261 return ff_vorbis_channel_layout_offsets[nb_channels - 1][channel_idx]; 1262 } 1263 1264 static int channel_reorder_unknown(int nb_channels, int channel_idx) 1265 { 1266 return channel_idx; 1267 } 1268 1269 1270 int ff_opus_parse_extradata (AVCtx* avctx, OpusContext* s, short cmtgain) { 1271 static immutable ubyte[2] default_channel_map = [ 0, 1 ]; 1272 1273 int function (int, int) nothrow @nogc channel_reorder = &channel_reorder_unknown; 1274 1275 const(uint8_t)* extradata, channel_map; 1276 int extradata_size; 1277 int ver, channels, map_type, streams, stereo_streams, i, j; 1278 uint64_t layout; 1279 1280 if (!avctx.extradata) { 1281 if (avctx.channels > 2) { 1282 //conlog("Multichannel configuration without extradata."); 1283 return AVERROR(EINVAL); 1284 } 1285 extradata = opus_default_extradata.ptr; 1286 extradata_size = cast(uint)opus_default_extradata.length; 1287 } else { 1288 extradata = avctx.extradata; 1289 extradata_size = avctx.extradata_size; 1290 } 1291 1292 if (extradata_size < 19) { 1293 //conlog("Invalid extradata size: ", extradata_size); 1294 return AVERROR_INVALIDDATA; 1295 } 1296 1297 ver = extradata[8]; 1298 if (ver > 15) { 1299 //conlog("Extradata version ", ver); 1300 return AVERROR_PATCHWELCOME; 1301 } 1302 1303 avctx.delay = AV_RL16(extradata + 10); 1304 1305 channels = avctx.extradata ? extradata[9] : (avctx.channels == 1) ? 1 : 2; 1306 if (!channels) { 1307 //conlog("Zero channel count specified in the extradata"); 1308 return AVERROR_INVALIDDATA; 1309 } 1310 1311 int ii = AV_RL16(extradata + 16); 1312 ii += cmtgain; 1313 if (ii < short.min) ii = short.min; else if (ii > short.max) ii = short.max; 1314 1315 s.gain_i = cast(short)ii; 1316 if (s.gain_i) s.gain = ff_exp10(s.gain_i / (20.0 * 256)); 1317 1318 map_type = extradata[18]; 1319 if (!map_type) { 1320 if (channels > 2) { 1321 //conlog("Channel mapping 0 is only specified for up to 2 channels"); 1322 return AVERROR_INVALIDDATA; 1323 } 1324 layout = (channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; 1325 streams = 1; 1326 stereo_streams = channels - 1; 1327 channel_map = default_channel_map.ptr; 1328 } else if (map_type == 1 || map_type == 2 || map_type == 255) { 1329 if (extradata_size < 21 + channels) { 1330 //conlog("Invalid extradata size: ", extradata_size); 1331 return AVERROR_INVALIDDATA; 1332 } 1333 1334 streams = extradata[19]; 1335 stereo_streams = extradata[20]; 1336 if (!streams || stereo_streams > streams || streams + stereo_streams > 255) { 1337 //conlog("Invalid stream/stereo stream count: ", streams, "/", stereo_streams); 1338 return AVERROR_INVALIDDATA; 1339 } 1340 1341 if (map_type == 1) { 1342 if (channels > 8) { 1343 //conlog("Channel mapping 1 is only specified for up to 8 channels"); 1344 return AVERROR_INVALIDDATA; 1345 } 1346 layout = ff_vorbis_channel_layouts[channels - 1]; 1347 //!channel_reorder = channel_reorder_vorbis; 1348 } else if (map_type == 2) { 1349 int ambisonic_order = ff_sqrt(channels) - 1; 1350 if (channels != (ambisonic_order + 1) * (ambisonic_order + 1)) { 1351 //conlog("Channel mapping 2 is only specified for channel counts which can be written as (n + 1)^2 for nonnegative integer n"); 1352 return AVERROR_INVALIDDATA; 1353 } 1354 layout = 0; 1355 } else { 1356 layout = 0; 1357 } 1358 1359 channel_map = extradata + 21; 1360 } else { 1361 //conlog("Mapping type ", map_type); 1362 return AVERROR_PATCHWELCOME; 1363 } 1364 1365 s.channel_maps = av_mallocz_array!(typeof(s.channel_maps[0]))(channels); 1366 if (s.channel_maps is null) return AVERROR(ENOMEM); 1367 1368 for (i = 0; i < channels; i++) { 1369 ChannelMap* map = &s.channel_maps[i]; 1370 uint8_t idx = channel_map[channel_reorder(channels, i)]; 1371 1372 if (idx == 255) { 1373 map.silence = 1; 1374 continue; 1375 } else if (idx >= streams + stereo_streams) { 1376 //conlog("Invalid channel map for output channel ", i, ": ", idx); 1377 return AVERROR_INVALIDDATA; 1378 } 1379 1380 // check that we did not see this index yet 1381 map.copy = 0; 1382 for (j = 0; j < i; j++) { 1383 if (channel_map[channel_reorder(channels, j)] == idx) { 1384 map.copy = 1; 1385 map.copy_idx = j; 1386 break; 1387 } 1388 } 1389 1390 if (idx < 2*stereo_streams) { 1391 map.stream_idx = idx/2; 1392 map.channel_idx = idx&1; 1393 } else { 1394 map.stream_idx = idx-stereo_streams; 1395 map.channel_idx = 0; 1396 } 1397 } 1398 1399 avctx.channels = channels; 1400 avctx.channel_layout = layout; 1401 s.nb_streams = streams; 1402 s.nb_stereo_streams = stereo_streams; 1403 1404 return 0; 1405 } 1406 1407 1408 struct IMDCT15Context { 1409 int fft_n; 1410 int len2; 1411 int len4; 1412 1413 FFTComplex* tmp; 1414 1415 FFTComplex* twiddle_exptab; 1416 1417 FFTComplex*[6] exptab; 1418 1419 /** 1420 * Calculate the middle half of the iMDCT 1421 */ 1422 void function (IMDCT15Context* s, float* dst, const(float)* src, ptrdiff_t src_stride, float scale) nothrow @nogc imdct_half; 1423 } 1424 1425 1426 // minimal iMDCT size to make SIMD opts easier 1427 enum CELT_MIN_IMDCT_SIZE = 120; 1428 1429 // complex c = a * b 1430 enum CMUL3(string cre, string cim, string are, string aim, string bre, string bim) = 1431 ""~cre~" = "~are~" * "~bre~" - "~aim~" * "~bim~";\n"~ 1432 ""~cim~" = "~are~" * "~bim~" + "~aim~" * "~bre~";\n"; 1433 1434 enum CMUL(string c, string a, string b) = CMUL3!("("~c~").re", "("~c~").im", "("~a~").re", "("~a~").im", "("~b~").re", "("~b~").im"); 1435 1436 // complex c = a * b 1437 // d = a * conjugate(b) 1438 enum CMUL2(string c, string d, string a, string b) = 1439 "{\n"~ 1440 "float are = ("~a~").re;\n"~ 1441 "float aim = ("~a~").im;\n"~ 1442 "float bre = ("~b~").re;\n"~ 1443 "float bim = ("~b~").im;\n"~ 1444 "float rr = are * bre;\n"~ 1445 "float ri = are * bim;\n"~ 1446 "float ir = aim * bre;\n"~ 1447 "float ii = aim * bim;\n"~ 1448 "("~c~").re = rr - ii;\n"~ 1449 "("~c~").im = ri + ir;\n"~ 1450 "("~d~").re = rr + ii;\n"~ 1451 "("~d~").im = -ri + ir;\n"~ 1452 "}\n"; 1453 1454 /*av_cold*/ void ff_imdct15_uninit (IMDCT15Context** ps) { 1455 IMDCT15Context* s = *ps; 1456 if (s is null) return; 1457 for (int i = 0; i < /*FF_ARRAY_ELEMS*/cast(int)s.exptab.length; ++i) av_freep(&s.exptab[i]); 1458 av_freep(&s.twiddle_exptab); 1459 av_freep(&s.tmp); 1460 av_freep(ps); 1461 } 1462 1463 //static void imdct15_half (IMDCT15Context* s, float* dst, const(float)* src, ptrdiff_t stride, float scale); 1464 1465 /*av_cold*/ int ff_imdct15_init (IMDCT15Context** ps, int N) { 1466 1467 1468 IMDCT15Context* s; 1469 int len2 = 15*(1<<N); 1470 int len = 2*len2; 1471 int i, j; 1472 1473 if (len2 > CELT_MAX_FRAME_SIZE || len2 < CELT_MIN_IMDCT_SIZE) return AVERROR(EINVAL); 1474 1475 s = av_mallocz!IMDCT15Context(); 1476 if (!s) return AVERROR(ENOMEM); 1477 1478 s.fft_n = N - 1; 1479 s.len4 = len2 / 2; 1480 s.len2 = len2; 1481 1482 s.tmp = av_malloc_array!(typeof(*s.tmp))(len); 1483 if (!s.tmp) goto fail; 1484 1485 s.twiddle_exptab = av_malloc_array!(typeof(*s.twiddle_exptab))(s.len4); 1486 if (!s.twiddle_exptab) goto fail; 1487 1488 for (i = 0; i < s.len4; i++) { 1489 s.twiddle_exptab[i].re = cos(2 * PI * (i + 0.125 + s.len4) / len); 1490 s.twiddle_exptab[i].im = sin(2 * PI * (i + 0.125 + s.len4) / len); 1491 } 1492 1493 for (i = 0; i < /*FF_ARRAY_ELEMS*/cast(int)s.exptab.length; i++) { 1494 int NN = 15 * (1 << i); 1495 s.exptab[i] = av_malloc!(typeof(*s.exptab[i]))(FFMAX(NN, 19)); 1496 if (!s.exptab[i]) goto fail; 1497 for (j = 0; j < NN; j++) { 1498 s.exptab[i][j].re = cos(2 * PI * j / NN); 1499 s.exptab[i][j].im = sin(2 * PI * j / NN); 1500 } 1501 } 1502 1503 // wrap around to simplify fft15 1504 for (j = 15; j < 19; j++) s.exptab[0][j] = s.exptab[0][j - 15]; 1505 1506 s.imdct_half = &imdct15_half; 1507 1508 //if (ARCH_AARCH64) ff_imdct15_init_aarch64(s); 1509 1510 *ps = s; 1511 1512 return 0; 1513 1514 fail: 1515 ff_imdct15_uninit(&s); 1516 return AVERROR(ENOMEM); 1517 } 1518 1519 1520 private void fft5(FFTComplex* out_, const(FFTComplex)* in_, ptrdiff_t stride) { 1521 // [0] = exp(2 * i * pi / 5), [1] = exp(2 * i * pi * 2 / 5) 1522 static immutable FFTComplex[2] fact = [ { 0.30901699437494745, 0.95105651629515353 }, 1523 { -0.80901699437494734, 0.58778525229247325 } ]; 1524 1525 FFTComplex[4][4] z; 1526 1527 mixin(CMUL2!("z[0][0]", "z[0][3]", "in_[1 * stride]", "fact[0]")); 1528 mixin(CMUL2!("z[0][1]", "z[0][2]", "in_[1 * stride]", "fact[1]")); 1529 mixin(CMUL2!("z[1][0]", "z[1][3]", "in_[2 * stride]", "fact[0]")); 1530 mixin(CMUL2!("z[1][1]", "z[1][2]", "in_[2 * stride]", "fact[1]")); 1531 mixin(CMUL2!("z[2][0]", "z[2][3]", "in_[3 * stride]", "fact[0]")); 1532 mixin(CMUL2!("z[2][1]", "z[2][2]", "in_[3 * stride]", "fact[1]")); 1533 mixin(CMUL2!("z[3][0]", "z[3][3]", "in_[4 * stride]", "fact[0]")); 1534 mixin(CMUL2!("z[3][1]", "z[3][2]", "in_[4 * stride]", "fact[1]")); 1535 1536 out_[0].re = in_[0].re + in_[stride].re + in_[2 * stride].re + in_[3 * stride].re + in_[4 * stride].re; 1537 out_[0].im = in_[0].im + in_[stride].im + in_[2 * stride].im + in_[3 * stride].im + in_[4 * stride].im; 1538 1539 out_[1].re = in_[0].re + z[0][0].re + z[1][1].re + z[2][2].re + z[3][3].re; 1540 out_[1].im = in_[0].im + z[0][0].im + z[1][1].im + z[2][2].im + z[3][3].im; 1541 1542 out_[2].re = in_[0].re + z[0][1].re + z[1][3].re + z[2][0].re + z[3][2].re; 1543 out_[2].im = in_[0].im + z[0][1].im + z[1][3].im + z[2][0].im + z[3][2].im; 1544 1545 out_[3].re = in_[0].re + z[0][2].re + z[1][0].re + z[2][3].re + z[3][1].re; 1546 out_[3].im = in_[0].im + z[0][2].im + z[1][0].im + z[2][3].im + z[3][1].im; 1547 1548 out_[4].re = in_[0].re + z[0][3].re + z[1][2].re + z[2][1].re + z[3][0].re; 1549 out_[4].im = in_[0].im + z[0][3].im + z[1][2].im + z[2][1].im + z[3][0].im; 1550 } 1551 1552 private void fft15 (IMDCT15Context* s, FFTComplex* out_, const(FFTComplex)* in_, ptrdiff_t stride) { 1553 const(FFTComplex)* exptab = s.exptab[0]; 1554 FFTComplex[5] tmp; 1555 FFTComplex[5] tmp1; 1556 FFTComplex[5] tmp2; 1557 int k; 1558 1559 fft5(tmp.ptr, in_, stride * 3); 1560 fft5(tmp1.ptr, in_ + stride, stride * 3); 1561 fft5(tmp2.ptr, in_ + 2 * stride, stride * 3); 1562 1563 for (k = 0; k < 5; k++) { 1564 FFTComplex t1, t2; 1565 1566 mixin(CMUL!("t1", "tmp1[k]", "exptab[k]")); 1567 mixin(CMUL!("t2", "tmp2[k]", "exptab[2 * k]")); 1568 out_[k].re = tmp[k].re + t1.re + t2.re; 1569 out_[k].im = tmp[k].im + t1.im + t2.im; 1570 1571 mixin(CMUL!("t1", "tmp1[k]", "exptab[k + 5]")); 1572 mixin(CMUL!("t2", "tmp2[k]", "exptab[2 * (k + 5)]")); 1573 out_[k + 5].re = tmp[k].re + t1.re + t2.re; 1574 out_[k + 5].im = tmp[k].im + t1.im + t2.im; 1575 1576 mixin(CMUL!("t1", "tmp1[k]", "exptab[k + 10]")); 1577 mixin(CMUL!("t2", "tmp2[k]", "exptab[2 * k + 5]")); 1578 out_[k + 10].re = tmp[k].re + t1.re + t2.re; 1579 out_[k + 10].im = tmp[k].im + t1.im + t2.im; 1580 } 1581 } 1582 1583 /* 1584 * FFT of the length 15 * (2^N) 1585 */ 1586 private void fft_calc (IMDCT15Context* s, FFTComplex* out_, const(FFTComplex)* in_, int N, ptrdiff_t stride) { 1587 if (N) { 1588 const(FFTComplex)* exptab = s.exptab[N]; 1589 const int len2 = 15 * (1 << (N - 1)); 1590 int k; 1591 1592 fft_calc(s, out_, in_, N - 1, stride * 2); 1593 fft_calc(s, out_ + len2, in_ + stride, N - 1, stride * 2); 1594 1595 for (k = 0; k < len2; k++) { 1596 FFTComplex t; 1597 1598 mixin(CMUL!("t", "out_[len2 + k]", "exptab[k]")); 1599 1600 out_[len2 + k].re = out_[k].re - t.re; 1601 out_[len2 + k].im = out_[k].im - t.im; 1602 1603 out_[k].re += t.re; 1604 out_[k].im += t.im; 1605 } 1606 } else { 1607 fft15(s, out_, in_, stride); 1608 } 1609 } 1610 1611 private void imdct15_half (IMDCT15Context* s, float* dst, const(float)* src, ptrdiff_t stride, float scale) { 1612 FFTComplex *z = cast(FFTComplex *)dst; 1613 const int len8 = s.len4 / 2; 1614 const(float)* in1 = src; 1615 const(float)* in2 = src + (s.len2 - 1) * stride; 1616 int i; 1617 1618 for (i = 0; i < s.len4; i++) { 1619 FFTComplex tmp = { *in2, *in1 }; 1620 mixin(CMUL!("s.tmp[i]", "tmp", "s.twiddle_exptab[i]")); 1621 in1 += 2 * stride; 1622 in2 -= 2 * stride; 1623 } 1624 1625 fft_calc(s, z, s.tmp, s.fft_n, 1); 1626 1627 for (i = 0; i < len8; i++) { 1628 float r0, i0, r1, i1; 1629 1630 mixin(CMUL3!("r0", "i1", "z[len8 - i - 1].im", "z[len8 - i - 1].re", "s.twiddle_exptab[len8 - i - 1].im", "s.twiddle_exptab[len8 - i - 1].re")); 1631 mixin(CMUL3!("r1", "i0", "z[len8 + i].im", "z[len8 + i].re", "s.twiddle_exptab[len8 + i].im", "s.twiddle_exptab[len8 + i].re")); 1632 z[len8 - i - 1].re = scale * r0; 1633 z[len8 - i - 1].im = scale * i0; 1634 z[len8 + i].re = scale * r1; 1635 z[len8 + i].im = scale * i1; 1636 } 1637 } 1638 1639 alias CeltSpread = int; 1640 enum /*CeltSpread*/:int { 1641 CELT_SPREAD_NONE, 1642 CELT_SPREAD_LIGHT, 1643 CELT_SPREAD_NORMAL, 1644 CELT_SPREAD_AGGRESSIVE 1645 } 1646 1647 struct CeltFrame { 1648 float[CELT_MAX_BANDS] energy; 1649 float[CELT_MAX_BANDS][2] prev_energy; 1650 1651 uint8_t[CELT_MAX_BANDS] collapse_masks; 1652 1653 /* buffer for mdct output + postfilter */ 1654 //DECLARE_ALIGNED(32, float, buf)[2048]; 1655 float[2048] buf; 1656 1657 /* postfilter parameters */ 1658 int pf_period_new; 1659 float[3] pf_gains_new; 1660 int pf_period; 1661 float[3] pf_gains; 1662 int pf_period_old; 1663 float[3] pf_gains_old; 1664 1665 float deemph_coeff; 1666 } 1667 1668 struct CeltContext { 1669 // constant values that do not change during context lifetime 1670 //AVCodecContext *avctx; 1671 IMDCT15Context*[4] imdct; 1672 //AVFloatDSPContext* dsp; 1673 int output_channels; 1674 1675 // values that have inter-frame effect and must be reset on flush 1676 CeltFrame[2] frame; 1677 uint32_t seed; 1678 int flushed; 1679 1680 // values that only affect a single frame 1681 int coded_channels; 1682 int framebits; 1683 int duration; 1684 1685 /* number of iMDCT blocks in the frame */ 1686 int blocks; 1687 /* size of each block */ 1688 int blocksize; 1689 1690 int startband; 1691 int endband; 1692 int codedbands; 1693 1694 int anticollapse_bit; 1695 1696 int intensitystereo; 1697 int dualstereo; 1698 CeltSpread spread; 1699 1700 int remaining; 1701 int remaining2; 1702 int[CELT_MAX_BANDS] fine_bits; 1703 int[CELT_MAX_BANDS] fine_priority; 1704 int[CELT_MAX_BANDS] pulses; 1705 int[CELT_MAX_BANDS] tf_change; 1706 1707 //DECLARE_ALIGNED(32, float, coeffs)[2][CELT_MAX_FRAME_SIZE]; 1708 //DECLARE_ALIGNED(32, float, scratch)[22 * 8]; // MAX(celt_freq_range) * 1<<CELT_MAX_LOG_BLOCKS 1709 float[CELT_MAX_FRAME_SIZE][2] coeffs; 1710 float[22 * 8] scratch; 1711 } 1712 1713 static immutable uint16_t[4] celt_model_tapset = [ 4, 2, 3, 4 ]; 1714 1715 static immutable uint16_t[5] celt_model_spread = [ 32, 7, 9, 30, 32 ]; 1716 1717 static immutable uint16_t[12] celt_model_alloc_trim = [ 1718 128, 2, 4, 9, 19, 41, 87, 109, 119, 124, 126, 128 1719 ]; 1720 1721 static immutable uint16_t[4] celt_model_energy_small = [ 4, 2, 3, 4 ]; 1722 1723 static immutable uint8_t[22] celt_freq_bands = [ /* in steps of 200Hz */ 1724 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 34, 40, 48, 60, 78, 100 1725 ]; 1726 1727 static immutable uint8_t[21] celt_freq_range = [ 1728 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 6, 6, 8, 12, 18, 22 1729 ]; 1730 1731 static immutable uint8_t[21] celt_log_freq_range = [ 1732 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36 1733 ]; 1734 1735 static immutable int8_t[2][2][2][4] celt_tf_select = [ 1736 [ [ [ 0, -1 ], [ 0, -1 ] ], [ [ 0, -1 ], [ 0, -1 ] ] ], 1737 [ [ [ 0, -1 ], [ 0, -2 ] ], [ [ 1, 0 ], [ 1, -1 ] ] ], 1738 [ [ [ 0, -2 ], [ 0, -3 ] ], [ [ 2, 0 ], [ 1, -1 ] ] ], 1739 [ [ [ 0, -2 ], [ 0, -3 ] ], [ [ 3, 0 ], [ 1, -1 ] ] ] 1740 ]; 1741 1742 static immutable float[25] celt_mean_energy = [ 1743 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, 1744 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, 1745 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, 1746 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, 1747 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f 1748 ]; 1749 1750 static immutable float[4] celt_alpha_coef = [ 1751 29440.0f/32768.0f, 26112.0f/32768.0f, 21248.0f/32768.0f, 16384.0f/32768.0f 1752 ]; 1753 1754 static immutable float[4] celt_beta_coef = [ /* TODO: precompute 1 minus this if the code ends up neater */ 1755 30147.0f/32768.0f, 22282.0f/32768.0f, 12124.0f/32768.0f, 6554.0f/32768.0f 1756 ]; 1757 1758 static immutable uint8_t[42][2][4] celt_coarse_energy_dist = [ 1759 [ 1760 [ // 120-sample inter 1761 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 1762 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, 1763 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11 1764 ], [ // 120-sample intra 1765 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, 1766 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, 1767 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50 1768 ] 1769 ], [ 1770 [ // 240-sample inter 1771 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, 1772 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, 1773 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9 1774 ], [ // 240-sample intra 1775 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 1776 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, 1777 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45 1778 ] 1779 ], [ 1780 [ // 480-sample inter 1781 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 1782 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, 1783 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10 1784 ], [ // 480-sample intra 1785 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, 1786 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, 1787 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42 1788 ] 1789 ], [ 1790 [ // 960-sample inter 1791 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, 1792 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, 1793 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15 1794 ], [ // 960-sample intra 1795 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 1796 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, 1797 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40 1798 ] 1799 ] 1800 ]; 1801 1802 static immutable uint8_t[21][11] celt_static_alloc = [ /* 1/32 bit/sample */ 1803 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 1804 [ 90, 80, 75, 69, 63, 56, 49, 40, 34, 29, 20, 18, 10, 0, 0, 0, 0, 0, 0, 0, 0 ], 1805 [ 110, 100, 90, 84, 78, 71, 65, 58, 51, 45, 39, 32, 26, 20, 12, 0, 0, 0, 0, 0, 0 ], 1806 [ 118, 110, 103, 93, 86, 80, 75, 70, 65, 59, 53, 47, 40, 31, 23, 15, 4, 0, 0, 0, 0 ], 1807 [ 126, 119, 112, 104, 95, 89, 83, 78, 72, 66, 60, 54, 47, 39, 32, 25, 17, 12, 1, 0, 0 ], 1808 [ 134, 127, 120, 114, 103, 97, 91, 85, 78, 72, 66, 60, 54, 47, 41, 35, 29, 23, 16, 10, 1 ], 1809 [ 144, 137, 130, 124, 113, 107, 101, 95, 88, 82, 76, 70, 64, 57, 51, 45, 39, 33, 26, 15, 1 ], 1810 [ 152, 145, 138, 132, 123, 117, 111, 105, 98, 92, 86, 80, 74, 67, 61, 55, 49, 43, 36, 20, 1 ], 1811 [ 162, 155, 148, 142, 133, 127, 121, 115, 108, 102, 96, 90, 84, 77, 71, 65, 59, 53, 46, 30, 1 ], 1812 [ 172, 165, 158, 152, 143, 137, 131, 125, 118, 112, 106, 100, 94, 87, 81, 75, 69, 63, 56, 45, 20 ], 1813 [ 200, 200, 200, 200, 200, 200, 200, 200, 198, 193, 188, 183, 178, 173, 168, 163, 158, 153, 148, 129, 104 ] 1814 ]; 1815 1816 static immutable uint8_t[21][2][4] celt_static_caps = [ 1817 [ // 120-sample 1818 [224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 1819 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37], 1820 [224, 224, 224, 224, 224, 224, 224, 224, 240, 240, 1821 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40], 1822 ], [ // 240-sample 1823 [160, 160, 160, 160, 160, 160, 160, 160, 185, 185, 1824 185, 185, 193, 193, 193, 183, 183, 172, 138, 64, 38], 1825 [240, 240, 240, 240, 240, 240, 240, 240, 207, 207, 1826 207, 207, 204, 204, 204, 193, 193, 180, 143, 66, 40], 1827 ], [ // 480-sample 1828 [185, 185, 185, 185, 185, 185, 185, 185, 193, 193, 1829 193, 193, 193, 193, 193, 183, 183, 172, 138, 65, 39], 1830 [207, 207, 207, 207, 207, 207, 207, 207, 204, 204, 1831 204, 204, 201, 201, 201, 188, 188, 176, 141, 66, 40], 1832 ], [ // 960-sample 1833 [193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 1834 193, 193, 194, 194, 194, 184, 184, 173, 139, 65, 39], 1835 [204, 204, 204, 204, 204, 204, 204, 204, 201, 201, 1836 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40] 1837 ] 1838 ]; 1839 1840 static immutable uint8_t[392] celt_cache_bits = [ 1841 40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 1842 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 1843 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 1844 31, 34, 36, 38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, 1845 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62, 63, 63, 65, 1846 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, 1847 64, 66, 69, 71, 73, 75, 76, 78, 80, 82, 85, 87, 89, 91, 92, 1848 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123, 1849 124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 1850 97, 100, 102, 105, 107, 111, 115, 118, 121, 124, 126, 129, 131, 135, 139, 1851 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, 1852 28, 49, 65, 78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 1853 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211, 216, 220, 225, 1854 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, 1855 166, 174, 182, 189, 195, 201, 207, 217, 227, 235, 243, 251, 17, 35, 63, 1856 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250, 1857 25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 1858 185, 190, 200, 208, 215, 222, 229, 235, 240, 245, 255, 16, 36, 65, 89, 1859 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, 1860 74, 103, 128, 151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 1861 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164, 182, 198, 214, 1862 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, 1863 90, 127, 160, 191, 220, 247, 6, 51, 95, 134, 170, 203, 234, 7, 47, 1864 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57, 1865 106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 1866 224, 5, 60, 113, 161, 206, 248, 4, 65, 122, 175, 224, 4, 67, 127, 1867 182, 234 1868 ]; 1869 1870 static immutable int16_t[105] celt_cache_index = [ 1871 -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, 1872 82, 82, 123, 164, 200, 222, 0, 0, 0, 0, 0, 0, 0, 0, 41, 1873 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, 1874 41, 41, 41, 41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, 1875 318, 328, 336, 123, 123, 123, 123, 123, 123, 123, 123, 240, 240, 240, 240, 1876 305, 305, 305, 318, 318, 343, 351, 358, 364, 240, 240, 240, 240, 240, 240, 1877 240, 240, 305, 305, 305, 305, 343, 343, 343, 351, 351, 370, 376, 382, 387, 1878 ]; 1879 1880 static immutable uint8_t[24] celt_log2_frac = [ 1881 0, 8, 13, 16, 19, 21, 23, 24, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 34, 35, 36, 36, 37, 37 1882 ]; 1883 1884 static immutable uint8_t[16] celt_bit_interleave = [ 1885 0, 1, 1, 1, 2, 3, 3, 3, 2, 3, 3, 3, 2, 3, 3, 3 1886 ]; 1887 1888 static immutable uint8_t[16] celt_bit_deinterleave = [ 1889 0x00, 0x03, 0x0C, 0x0F, 0x30, 0x33, 0x3C, 0x3F, 1890 0xC0, 0xC3, 0xCC, 0xCF, 0xF0, 0xF3, 0xFC, 0xFF 1891 ]; 1892 1893 static immutable uint8_t[30] celt_hadamard_ordery = [ 1894 1, 0, 1895 3, 0, 2, 1, 1896 7, 0, 4, 3, 6, 1, 5, 2, 1897 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5 1898 ]; 1899 1900 static immutable uint16_t[8] celt_qn_exp2 = [ 1901 16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048 1902 ]; 1903 1904 static immutable uint32_t[1272] celt_pvq_u = [ 1905 /* N = 0, K = 0...176 */ 1906 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1907 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1908 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1909 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1910 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1911 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1912 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1913 /* N = 1, K = 1...176 */ 1914 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1915 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1916 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1917 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1918 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1919 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1920 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1921 /* N = 2, K = 2...176 */ 1922 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 1923 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 1924 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 1925 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 1926 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 1927 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 1928 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 1929 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, 1930 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 285, 287, 289, 291, 293, 1931 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315, 317, 319, 321, 323, 1932 325, 327, 329, 331, 333, 335, 337, 339, 341, 343, 345, 347, 349, 351, 1933 /* N = 3, K = 3...176 */ 1934 13, 25, 41, 61, 85, 113, 145, 181, 221, 265, 313, 365, 421, 481, 545, 613, 1935 685, 761, 841, 925, 1013, 1105, 1201, 1301, 1405, 1513, 1625, 1741, 1861, 1936 1985, 2113, 2245, 2381, 2521, 2665, 2813, 2965, 3121, 3281, 3445, 3613, 3785, 1937 3961, 4141, 4325, 4513, 4705, 4901, 5101, 5305, 5513, 5725, 5941, 6161, 6385, 1938 6613, 6845, 7081, 7321, 7565, 7813, 8065, 8321, 8581, 8845, 9113, 9385, 9661, 1939 9941, 10225, 10513, 10805, 11101, 11401, 11705, 12013, 12325, 12641, 12961, 1940 13285, 13613, 13945, 14281, 14621, 14965, 15313, 15665, 16021, 16381, 16745, 1941 17113, 17485, 17861, 18241, 18625, 19013, 19405, 19801, 20201, 20605, 21013, 1942 21425, 21841, 22261, 22685, 23113, 23545, 23981, 24421, 24865, 25313, 25765, 1943 26221, 26681, 27145, 27613, 28085, 28561, 29041, 29525, 30013, 30505, 31001, 1944 31501, 32005, 32513, 33025, 33541, 34061, 34585, 35113, 35645, 36181, 36721, 1945 37265, 37813, 38365, 38921, 39481, 40045, 40613, 41185, 41761, 42341, 42925, 1946 43513, 44105, 44701, 45301, 45905, 46513, 47125, 47741, 48361, 48985, 49613, 1947 50245, 50881, 51521, 52165, 52813, 53465, 54121, 54781, 55445, 56113, 56785, 1948 57461, 58141, 58825, 59513, 60205, 60901, 61601, 1949 /* N = 4, K = 4...176 */ 1950 63, 129, 231, 377, 575, 833, 1159, 1561, 2047, 2625, 3303, 4089, 4991, 6017, 1951 7175, 8473, 9919, 11521, 13287, 15225, 17343, 19649, 22151, 24857, 27775, 1952 30913, 34279, 37881, 41727, 45825, 50183, 54809, 59711, 64897, 70375, 76153, 1953 82239, 88641, 95367, 102425, 109823, 117569, 125671, 134137, 142975, 152193, 1954 161799, 171801, 182207, 193025, 204263, 215929, 228031, 240577, 253575, 1955 267033, 280959, 295361, 310247, 325625, 341503, 357889, 374791, 392217, 1956 410175, 428673, 447719, 467321, 487487, 508225, 529543, 551449, 573951, 1957 597057, 620775, 645113, 670079, 695681, 721927, 748825, 776383, 804609, 1958 833511, 863097, 893375, 924353, 956039, 988441, 1021567, 1055425, 1090023, 1959 1125369, 1161471, 1198337, 1235975, 1274393, 1313599, 1353601, 1394407, 1960 1436025, 1478463, 1521729, 1565831, 1610777, 1656575, 1703233, 1750759, 1961 1799161, 1848447, 1898625, 1949703, 2001689, 2054591, 2108417, 2163175, 1962 2218873, 2275519, 2333121, 2391687, 2451225, 2511743, 2573249, 2635751, 1963 2699257, 2763775, 2829313, 2895879, 2963481, 3032127, 3101825, 3172583, 1964 3244409, 3317311, 3391297, 3466375, 3542553, 3619839, 3698241, 3777767, 1965 3858425, 3940223, 4023169, 4107271, 4192537, 4278975, 4366593, 4455399, 1966 4545401, 4636607, 4729025, 4822663, 4917529, 5013631, 5110977, 5209575, 1967 5309433, 5410559, 5512961, 5616647, 5721625, 5827903, 5935489, 6044391, 1968 6154617, 6266175, 6379073, 6493319, 6608921, 6725887, 6844225, 6963943, 1969 7085049, 7207551, 1970 /* N = 5, K = 5...176 */ 1971 321, 681, 1289, 2241, 3649, 5641, 8361, 11969, 16641, 22569, 29961, 39041, 1972 50049, 63241, 78889, 97281, 118721, 143529, 172041, 204609, 241601, 283401, 1973 330409, 383041, 441729, 506921, 579081, 658689, 746241, 842249, 947241, 1974 1061761, 1186369, 1321641, 1468169, 1626561, 1797441, 1981449, 2179241, 1975 2391489, 2618881, 2862121, 3121929, 3399041, 3694209, 4008201, 4341801, 1976 4695809, 5071041, 5468329, 5888521, 6332481, 6801089, 7295241, 7815849, 1977 8363841, 8940161, 9545769, 10181641, 10848769, 11548161, 12280841, 13047849, 1978 13850241, 14689089, 15565481, 16480521, 17435329, 18431041, 19468809, 1979 20549801, 21675201, 22846209, 24064041, 25329929, 26645121, 28010881, 1980 29428489, 30899241, 32424449, 34005441, 35643561, 37340169, 39096641, 1981 40914369, 42794761, 44739241, 46749249, 48826241, 50971689, 53187081, 1982 55473921, 57833729, 60268041, 62778409, 65366401, 68033601, 70781609, 1983 73612041, 76526529, 79526721, 82614281, 85790889, 89058241, 92418049, 1984 95872041, 99421961, 103069569, 106816641, 110664969, 114616361, 118672641, 1985 122835649, 127107241, 131489289, 135983681, 140592321, 145317129, 150160041, 1986 155123009, 160208001, 165417001, 170752009, 176215041, 181808129, 187533321, 1987 193392681, 199388289, 205522241, 211796649, 218213641, 224775361, 231483969, 1988 238341641, 245350569, 252512961, 259831041, 267307049, 274943241, 282741889, 1989 290705281, 298835721, 307135529, 315607041, 324252609, 333074601, 342075401, 1990 351257409, 360623041, 370174729, 379914921, 389846081, 399970689, 410291241, 1991 420810249, 431530241, 442453761, 453583369, 464921641, 476471169, 488234561, 1992 500214441, 512413449, 524834241, 537479489, 550351881, 563454121, 576788929, 1993 590359041, 604167209, 618216201, 632508801, 1994 /* N = 6, K = 6...96 (technically V(109,5) fits in 32 bits, but that can't be 1995 achieved by splitting an Opus band) */ 1996 1683, 3653, 7183, 13073, 22363, 36365, 56695, 85305, 124515, 177045, 246047, 1997 335137, 448427, 590557, 766727, 982729, 1244979, 1560549, 1937199, 2383409, 1998 2908411, 3522221, 4235671, 5060441, 6009091, 7095093, 8332863, 9737793, 1999 11326283, 13115773, 15124775, 17372905, 19880915, 22670725, 25765455, 2000 29189457, 32968347, 37129037, 41699767, 46710137, 52191139, 58175189, 2001 64696159, 71789409, 79491819, 87841821, 96879431, 106646281, 117185651, 2002 128542501, 140763503, 153897073, 167993403, 183104493, 199284183, 216588185, 2003 235074115, 254801525, 275831935, 298228865, 322057867, 347386557, 374284647, 2004 402823977, 433078547, 465124549, 499040399, 534906769, 572806619, 612825229, 2005 655050231, 699571641, 746481891, 795875861, 847850911, 902506913, 959946283, 2006 1020274013, 1083597703, 1150027593, 1219676595, 1292660325, 1369097135, 2007 1449108145, 1532817275, 1620351277, 1711839767, 1807415257, 1907213187, 2008 2011371957, 2120032959, 2009 /* N = 7, K = 7...54 (technically V(60,6) fits in 32 bits, but that can't be 2010 achieved by splitting an Opus band) */ 2011 8989, 19825, 40081, 75517, 134245, 227305, 369305, 579125, 880685, 1303777, 2012 1884961, 2668525, 3707509, 5064793, 6814249, 9041957, 11847485, 15345233, 2013 19665841, 24957661, 31388293, 39146185, 48442297, 59511829, 72616013, 2014 88043969, 106114625, 127178701, 151620757, 179861305, 212358985, 249612805, 2015 292164445, 340600625, 395555537, 457713341, 527810725, 606639529, 695049433, 2016 793950709, 904317037, 1027188385, 1163673953, 1314955181, 1482288821, 2017 1667010073, 1870535785, 2094367717, 2018 /* N = 8, K = 8...37 (technically V(40,7) fits in 32 bits, but that can't be 2019 achieved by splitting an Opus band) */ 2020 48639, 108545, 224143, 433905, 795455, 1392065, 2340495, 3800305, 5984767, 2021 9173505, 13726991, 20103025, 28875327, 40754369, 56610575, 77500017, 2022 104692735, 139703809, 184327311, 240673265, 311207743, 398796225, 506750351, 2023 638878193, 799538175, 993696769, 1226990095, 1505789553, 1837271615, 2024 2229491905, 2025 /* N = 9, K = 9...28 (technically V(29,8) fits in 32 bits, but that can't be 2026 achieved by splitting an Opus band) */ 2027 265729, 598417, 1256465, 2485825, 4673345, 8405905, 14546705, 24331777, 2028 39490049, 62390545, 96220561, 145198913, 214828609, 312193553, 446304145, 2029 628496897, 872893441, 1196924561, 1621925137, 2173806145, 2030 /* N = 10, K = 10...24 */ 2031 1462563, 3317445, 7059735, 14218905, 27298155, 50250765, 89129247, 152951073, 2032 254831667, 413442773, 654862247, 1014889769, 1541911931, 2300409629, 2033 3375210671, 2034 /* N = 11, K = 11...19 (technically V(20,10) fits in 32 bits, but that can't be 2035 achieved by splitting an Opus band) */ 2036 8097453, 18474633, 39753273, 81270333, 158819253, 298199265, 540279585, 2037 948062325, 1616336765, 2038 /* N = 12, K = 12...18 */ 2039 45046719, 103274625, 224298231, 464387817, 921406335, 1759885185, 2040 3248227095, 2041 /* N = 13, K = 13...16 */ 2042 251595969, 579168825, 1267854873, 2653649025, 2043 /* N = 14, K = 14 */ 2044 1409933619 2045 ]; 2046 2047 //DECLARE_ALIGNED(32, static immutable float, celt_window)[120] = [ 2048 static immutable float[120] celt_window = [ 2049 6.7286966e-05f, 0.00060551348f, 0.0016815970f, 0.0032947962f, 0.0054439943f, 2050 0.0081276923f, 0.011344001f, 0.015090633f, 0.019364886f, 0.024163635f, 2051 0.029483315f, 0.035319905f, 0.041668911f, 0.048525347f, 0.055883718f, 2052 0.063737999f, 0.072081616f, 0.080907428f, 0.090207705f, 0.099974111f, 2053 0.11019769f, 0.12086883f, 0.13197729f, 0.14351214f, 0.15546177f, 2054 0.16781389f, 0.18055550f, 0.19367290f, 0.20715171f, 0.22097682f, 2055 0.23513243f, 0.24960208f, 0.26436860f, 0.27941419f, 0.29472040f, 2056 0.31026818f, 0.32603788f, 0.34200931f, 0.35816177f, 0.37447407f, 2057 0.39092462f, 0.40749142f, 0.42415215f, 0.44088423f, 0.45766484f, 2058 0.47447104f, 0.49127978f, 0.50806798f, 0.52481261f, 0.54149077f, 2059 0.55807973f, 0.57455701f, 0.59090049f, 0.60708841f, 0.62309951f, 2060 0.63891306f, 0.65450896f, 0.66986776f, 0.68497077f, 0.69980010f, 2061 0.71433873f, 0.72857055f, 0.74248043f, 0.75605424f, 0.76927895f, 2062 0.78214257f, 0.79463430f, 0.80674445f, 0.81846456f, 0.82978733f, 2063 0.84070669f, 0.85121779f, 0.86131698f, 0.87100183f, 0.88027111f, 2064 0.88912479f, 0.89756398f, 0.90559094f, 0.91320904f, 0.92042270f, 2065 0.92723738f, 0.93365955f, 0.93969656f, 0.94535671f, 0.95064907f, 2066 0.95558353f, 0.96017067f, 0.96442171f, 0.96834849f, 0.97196334f, 2067 0.97527906f, 0.97830883f, 0.98106616f, 0.98356480f, 0.98581869f, 2068 0.98784191f, 0.98964856f, 0.99125274f, 0.99266849f, 0.99390969f, 2069 0.99499004f, 0.99592297f, 0.99672162f, 0.99739874f, 0.99796667f, 2070 0.99843728f, 0.99882195f, 0.99913147f, 0.99937606f, 0.99956527f, 2071 0.99970802f, 0.99981248f, 0.99988613f, 0.99993565f, 0.99996697f, 2072 0.99998518f, 0.99999457f, 0.99999859f, 0.99999982f, 1.0000000f, 2073 ]; 2074 2075 /* square of the window, used for the postfilter */ 2076 static immutable float[120] ff_celt_window2 = [ 2077 4.5275357e-09f, 3.66647e-07f, 2.82777e-06f, 1.08557e-05f, 2.96371e-05f, 6.60594e-05f, 2078 0.000128686f, 0.000227727f, 0.000374999f, 0.000583881f, 0.000869266f, 0.0012475f, 2079 0.0017363f, 0.00235471f, 0.00312299f, 0.00406253f, 0.00519576f, 0.00654601f, 2080 0.00813743f, 0.00999482f, 0.0121435f, 0.0146093f, 0.017418f, 0.0205957f, 0.0241684f, 2081 0.0281615f, 0.0326003f, 0.0375092f, 0.0429118f, 0.0488308f, 0.0552873f, 0.0623012f, 2082 0.0698908f, 0.0780723f, 0.0868601f, 0.0962664f, 0.106301f, 0.11697f, 0.12828f, 2083 0.140231f, 0.152822f, 0.166049f, 0.179905f, 0.194379f, 0.209457f, 0.225123f, 0.241356f, 2084 0.258133f, 0.275428f, 0.293212f, 0.311453f, 0.330116f, 0.349163f, 0.368556f, 0.388253f, 2085 0.40821f, 0.428382f, 0.448723f, 0.469185f, 0.48972f, 0.51028f, 0.530815f, 0.551277f, 2086 0.571618f, 0.59179f, 0.611747f, 0.631444f, 0.650837f, 0.669884f, 0.688547f, 0.706788f, 2087 0.724572f, 0.741867f, 0.758644f, 0.774877f, 0.790543f, 0.805621f, 0.820095f, 0.833951f, 2088 0.847178f, 0.859769f, 0.87172f, 0.88303f, 0.893699f, 0.903734f, 0.91314f, 0.921928f, 2089 0.930109f, 0.937699f, 0.944713f, 0.951169f, 0.957088f, 0.962491f, 0.9674f, 0.971838f, 2090 0.975832f, 0.979404f, 0.982582f, 0.985391f, 0.987857f, 0.990005f, 0.991863f, 0.993454f, 2091 0.994804f, 0.995937f, 0.996877f, 0.997645f, 0.998264f, 0.998753f, 0.999131f, 0.999416f, 2092 0.999625f, 0.999772f, 0.999871f, 0.999934f, 0.99997f, 0.999989f, 0.999997f, 0.99999964f, 1.0f, 2093 ]; 2094 2095 static immutable uint32_t*[15] celt_pvq_u_row = [ 2096 celt_pvq_u.ptr + 0, celt_pvq_u.ptr + 176, celt_pvq_u.ptr + 351, 2097 celt_pvq_u.ptr + 525, celt_pvq_u.ptr + 698, celt_pvq_u.ptr + 870, 2098 celt_pvq_u.ptr + 1041, celt_pvq_u.ptr + 1131, celt_pvq_u.ptr + 1178, 2099 celt_pvq_u.ptr + 1207, celt_pvq_u.ptr + 1226, celt_pvq_u.ptr + 1240, 2100 celt_pvq_u.ptr + 1248, celt_pvq_u.ptr + 1254, celt_pvq_u.ptr + 1257 2101 ]; 2102 2103 /*static inline*/ int16_t celt_cos(int16_t x) 2104 { 2105 x = cast(short)((MUL16(x, x) + 4096) >> 13); 2106 x = cast(short)((32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x)))))); 2107 return cast(short)(1+x); 2108 } 2109 2110 /*static inline*/ int celt_log2tan(int isin, int icos) 2111 { 2112 int lc, ls; 2113 lc = opus_ilog(icos); 2114 ls = opus_ilog(isin); 2115 icos <<= 15 - lc; 2116 isin <<= 15 - ls; 2117 return (ls << 11) - (lc << 11) + 2118 ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) - 2119 ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932); 2120 } 2121 2122 /*static inline*/ uint32_t celt_rng(CeltContext *s) 2123 { 2124 s.seed = 1664525 * s.seed + 1013904223; 2125 return s.seed; 2126 } 2127 2128 private void celt_decode_coarse_energy(CeltContext *s, OpusRangeCoder *rc) 2129 { 2130 int i, j; 2131 float[2] prev = 0; 2132 float alpha, beta; 2133 const(uint8_t)* model; 2134 2135 /* use the 2D z-transform to apply prediction in both */ 2136 /* the time domain (alpha) and the frequency domain (beta) */ 2137 2138 if (opus_rc_tell(rc)+3 <= s.framebits && opus_rc_p2model(rc, 3)) { 2139 /* intra frame */ 2140 alpha = 0; 2141 beta = 1.0f - 4915.0f/32768.0f; 2142 model = celt_coarse_energy_dist[s.duration][1].ptr; 2143 } else { 2144 alpha = celt_alpha_coef[s.duration]; 2145 beta = 1.0f - celt_beta_coef[s.duration]; 2146 model = celt_coarse_energy_dist[s.duration][0].ptr; 2147 } 2148 2149 for (i = 0; i < CELT_MAX_BANDS; i++) { 2150 for (j = 0; j < s.coded_channels; j++) { 2151 CeltFrame *frame = &s.frame[j]; 2152 float value; 2153 int available; 2154 2155 if (i < s.startband || i >= s.endband) { 2156 frame.energy[i] = 0.0; 2157 continue; 2158 } 2159 2160 available = s.framebits - opus_rc_tell(rc); 2161 if (available >= 15) { 2162 /* decode using a Laplace distribution */ 2163 int k = FFMIN(i, 20) << 1; 2164 value = opus_rc_laplace(rc, model[k] << 7, model[k+1] << 6); 2165 } else if (available >= 2) { 2166 int x = opus_rc_getsymbol(rc, celt_model_energy_small.ptr); 2167 value = (x>>1) ^ -(x&1); 2168 } else if (available >= 1) { 2169 value = -cast(float)opus_rc_p2model(rc, 1); 2170 } else value = -1; 2171 2172 frame.energy[i] = FFMAX(-9.0f, frame.energy[i]) * alpha + prev[j] + value; 2173 prev[j] += beta * value; 2174 } 2175 } 2176 } 2177 2178 private void celt_decode_fine_energy(CeltContext *s, OpusRangeCoder *rc) 2179 { 2180 int i; 2181 for (i = s.startband; i < s.endband; i++) { 2182 int j; 2183 if (!s.fine_bits[i]) 2184 continue; 2185 2186 for (j = 0; j < s.coded_channels; j++) { 2187 CeltFrame *frame = &s.frame[j]; 2188 int q2; 2189 float offset; 2190 q2 = opus_getrawbits(rc, s.fine_bits[i]); 2191 offset = (q2 + 0.5f) * (1 << (14 - s.fine_bits[i])) / 16384.0f - 0.5f; 2192 frame.energy[i] += offset; 2193 } 2194 } 2195 } 2196 2197 private void celt_decode_final_energy(CeltContext *s, OpusRangeCoder *rc, int bits_left) 2198 { 2199 int priority, i, j; 2200 2201 for (priority = 0; priority < 2; priority++) { 2202 for (i = s.startband; i < s.endband && bits_left >= s.coded_channels; i++) { 2203 if (s.fine_priority[i] != priority || s.fine_bits[i] >= CELT_MAX_FINE_BITS) 2204 continue; 2205 2206 for (j = 0; j < s.coded_channels; j++) { 2207 int q2; 2208 float offset; 2209 q2 = opus_getrawbits(rc, 1); 2210 offset = (q2 - 0.5f) * (1 << (14 - s.fine_bits[i] - 1)) / 16384.0f; 2211 s.frame[j].energy[i] += offset; 2212 bits_left--; 2213 } 2214 } 2215 } 2216 } 2217 2218 private void celt_decode_tf_changes(CeltContext *s, OpusRangeCoder *rc, int transient) 2219 { 2220 int i, diff = 0, tf_select = 0, tf_changed = 0, tf_select_bit; 2221 int consumed, bits = transient ? 2 : 4; 2222 2223 consumed = opus_rc_tell(rc); 2224 tf_select_bit = (s.duration != 0 && consumed+bits+1 <= s.framebits); 2225 2226 for (i = s.startband; i < s.endband; i++) { 2227 if (consumed+bits+tf_select_bit <= s.framebits) { 2228 diff ^= opus_rc_p2model(rc, bits); 2229 consumed = opus_rc_tell(rc); 2230 tf_changed |= diff; 2231 } 2232 s.tf_change[i] = diff; 2233 bits = transient ? 4 : 5; 2234 } 2235 2236 if (tf_select_bit && celt_tf_select[s.duration][transient][0][tf_changed] != 2237 celt_tf_select[s.duration][transient][1][tf_changed]) 2238 tf_select = opus_rc_p2model(rc, 1); 2239 2240 for (i = s.startband; i < s.endband; i++) { 2241 s.tf_change[i] = celt_tf_select[s.duration][transient][tf_select][s.tf_change[i]]; 2242 } 2243 } 2244 2245 private void celt_decode_allocation(CeltContext *s, OpusRangeCoder *rc) 2246 { 2247 // approx. maximum bit allocation for each band before boost/trim 2248 int[CELT_MAX_BANDS] cap; 2249 int[CELT_MAX_BANDS] boost; 2250 int[CELT_MAX_BANDS] threshold; 2251 int[CELT_MAX_BANDS] bits1; 2252 int[CELT_MAX_BANDS] bits2; 2253 int[CELT_MAX_BANDS] trim_offset; 2254 2255 int skip_startband = s.startband; 2256 int dynalloc = 6; 2257 int alloctrim = 5; 2258 int extrabits = 0; 2259 2260 int skip_bit = 0; 2261 int intensitystereo_bit = 0; 2262 int dualstereo_bit = 0; 2263 2264 int remaining, bandbits; 2265 int low, high, total, done; 2266 int totalbits; 2267 int consumed; 2268 int i, j; 2269 2270 consumed = opus_rc_tell(rc); 2271 2272 /* obtain spread flag */ 2273 s.spread = CELT_SPREAD_NORMAL; 2274 if (consumed + 4 <= s.framebits) 2275 s.spread = opus_rc_getsymbol(rc, celt_model_spread.ptr); 2276 2277 /* generate static allocation caps */ 2278 for (i = 0; i < CELT_MAX_BANDS; i++) { 2279 cap[i] = (celt_static_caps[s.duration][s.coded_channels - 1][i] + 64) 2280 * celt_freq_range[i] << (s.coded_channels - 1) << s.duration >> 2; 2281 } 2282 2283 /* obtain band boost */ 2284 totalbits = s.framebits << 3; // convert to 1/8 bits 2285 consumed = opus_rc_tell_frac(rc); 2286 for (i = s.startband; i < s.endband; i++) { 2287 int quanta, band_dynalloc; 2288 2289 boost[i] = 0; 2290 2291 quanta = celt_freq_range[i] << (s.coded_channels - 1) << s.duration; 2292 quanta = FFMIN(quanta << 3, FFMAX(6 << 3, quanta)); 2293 band_dynalloc = dynalloc; 2294 while (consumed + (band_dynalloc<<3) < totalbits && boost[i] < cap[i]) { 2295 int add = opus_rc_p2model(rc, band_dynalloc); 2296 consumed = opus_rc_tell_frac(rc); 2297 if (!add) 2298 break; 2299 2300 boost[i] += quanta; 2301 totalbits -= quanta; 2302 band_dynalloc = 1; 2303 } 2304 /* dynalloc is more likely to occur if it's already been used for earlier bands */ 2305 if (boost[i]) 2306 dynalloc = FFMAX(2, dynalloc - 1); 2307 } 2308 2309 /* obtain allocation trim */ 2310 if (consumed + (6 << 3) <= totalbits) 2311 alloctrim = opus_rc_getsymbol(rc, celt_model_alloc_trim.ptr); 2312 2313 /* anti-collapse bit reservation */ 2314 totalbits = (s.framebits << 3) - opus_rc_tell_frac(rc) - 1; 2315 s.anticollapse_bit = 0; 2316 if (s.blocks > 1 && s.duration >= 2 && 2317 totalbits >= ((s.duration + 2) << 3)) 2318 s.anticollapse_bit = 1 << 3; 2319 totalbits -= s.anticollapse_bit; 2320 2321 /* band skip bit reservation */ 2322 if (totalbits >= 1 << 3) 2323 skip_bit = 1 << 3; 2324 totalbits -= skip_bit; 2325 2326 /* intensity/dual stereo bit reservation */ 2327 if (s.coded_channels == 2) { 2328 intensitystereo_bit = celt_log2_frac[s.endband - s.startband]; 2329 if (intensitystereo_bit <= totalbits) { 2330 totalbits -= intensitystereo_bit; 2331 if (totalbits >= 1 << 3) { 2332 dualstereo_bit = 1 << 3; 2333 totalbits -= 1 << 3; 2334 } 2335 } else 2336 intensitystereo_bit = 0; 2337 } 2338 2339 for (i = s.startband; i < s.endband; i++) { 2340 int trim = alloctrim - 5 - s.duration; 2341 int band = celt_freq_range[i] * (s.endband - i - 1); 2342 int duration = s.duration + 3; 2343 int scale = duration + s.coded_channels - 1; 2344 2345 /* PVQ minimum allocation threshold, below this value the band is 2346 * skipped */ 2347 threshold[i] = FFMAX(3 * celt_freq_range[i] << duration >> 4, 2348 s.coded_channels << 3); 2349 2350 trim_offset[i] = trim * (band << scale) >> 6; 2351 2352 if (celt_freq_range[i] << s.duration == 1) 2353 trim_offset[i] -= s.coded_channels << 3; 2354 } 2355 2356 /* bisection */ 2357 low = 1; 2358 high = CELT_VECTORS - 1; 2359 while (low <= high) { 2360 int center = (low + high) >> 1; 2361 done = total = 0; 2362 2363 for (i = s.endband - 1; i >= s.startband; i--) { 2364 bandbits = celt_freq_range[i] * celt_static_alloc[center][i] 2365 << (s.coded_channels - 1) << s.duration >> 2; 2366 2367 if (bandbits) 2368 bandbits = FFMAX(0, bandbits + trim_offset[i]); 2369 bandbits += boost[i]; 2370 2371 if (bandbits >= threshold[i] || done) { 2372 done = 1; 2373 total += FFMIN(bandbits, cap[i]); 2374 } else if (bandbits >= s.coded_channels << 3) 2375 total += s.coded_channels << 3; 2376 } 2377 2378 if (total > totalbits) 2379 high = center - 1; 2380 else 2381 low = center + 1; 2382 } 2383 high = low--; 2384 2385 for (i = s.startband; i < s.endband; i++) { 2386 bits1[i] = celt_freq_range[i] * celt_static_alloc[low][i] 2387 << (s.coded_channels - 1) << s.duration >> 2; 2388 bits2[i] = high >= CELT_VECTORS ? cap[i] : 2389 celt_freq_range[i] * celt_static_alloc[high][i] 2390 << (s.coded_channels - 1) << s.duration >> 2; 2391 2392 if (bits1[i]) 2393 bits1[i] = FFMAX(0, bits1[i] + trim_offset[i]); 2394 if (bits2[i]) 2395 bits2[i] = FFMAX(0, bits2[i] + trim_offset[i]); 2396 if (low) 2397 bits1[i] += boost[i]; 2398 bits2[i] += boost[i]; 2399 2400 if (boost[i]) 2401 skip_startband = i; 2402 bits2[i] = FFMAX(0, bits2[i] - bits1[i]); 2403 } 2404 2405 /* bisection */ 2406 low = 0; 2407 high = 1 << CELT_ALLOC_STEPS; 2408 for (i = 0; i < CELT_ALLOC_STEPS; i++) { 2409 int center = (low + high) >> 1; 2410 done = total = 0; 2411 2412 for (j = s.endband - 1; j >= s.startband; j--) { 2413 bandbits = bits1[j] + (center * bits2[j] >> CELT_ALLOC_STEPS); 2414 2415 if (bandbits >= threshold[j] || done) { 2416 done = 1; 2417 total += FFMIN(bandbits, cap[j]); 2418 } else if (bandbits >= s.coded_channels << 3) 2419 total += s.coded_channels << 3; 2420 } 2421 if (total > totalbits) 2422 high = center; 2423 else 2424 low = center; 2425 } 2426 2427 done = total = 0; 2428 for (i = s.endband - 1; i >= s.startband; i--) { 2429 bandbits = bits1[i] + (low * bits2[i] >> CELT_ALLOC_STEPS); 2430 2431 if (bandbits >= threshold[i] || done) 2432 done = 1; 2433 else 2434 bandbits = (bandbits >= s.coded_channels << 3) ? 2435 s.coded_channels << 3 : 0; 2436 2437 bandbits = FFMIN(bandbits, cap[i]); 2438 s.pulses[i] = bandbits; 2439 total += bandbits; 2440 } 2441 2442 /* band skipping */ 2443 for (s.codedbands = s.endband; ; s.codedbands--) { 2444 int allocation; 2445 j = s.codedbands - 1; 2446 2447 if (j == skip_startband) { 2448 /* all remaining bands are not skipped */ 2449 totalbits += skip_bit; 2450 break; 2451 } 2452 2453 /* determine the number of bits available for coding "do not skip" markers */ 2454 remaining = totalbits - total; 2455 bandbits = remaining / (celt_freq_bands[j+1] - celt_freq_bands[s.startband]); 2456 remaining -= bandbits * (celt_freq_bands[j+1] - celt_freq_bands[s.startband]); 2457 allocation = s.pulses[j] + bandbits * celt_freq_range[j] 2458 + FFMAX(0, remaining - (celt_freq_bands[j] - celt_freq_bands[s.startband])); 2459 2460 /* a "do not skip" marker is only coded if the allocation is 2461 above the chosen threshold */ 2462 if (allocation >= FFMAX(threshold[j], (s.coded_channels + 1) <<3 )) { 2463 if (opus_rc_p2model(rc, 1)) 2464 break; 2465 2466 total += 1 << 3; 2467 allocation -= 1 << 3; 2468 } 2469 2470 /* the band is skipped, so reclaim its bits */ 2471 total -= s.pulses[j]; 2472 if (intensitystereo_bit) { 2473 total -= intensitystereo_bit; 2474 intensitystereo_bit = celt_log2_frac[j - s.startband]; 2475 total += intensitystereo_bit; 2476 } 2477 2478 total += s.pulses[j] = (allocation >= s.coded_channels << 3) ? 2479 s.coded_channels << 3 : 0; 2480 } 2481 2482 /* obtain stereo flags */ 2483 s.intensitystereo = 0; 2484 s.dualstereo = 0; 2485 if (intensitystereo_bit) 2486 s.intensitystereo = s.startband + 2487 opus_rc_unimodel(rc, s.codedbands + 1 - s.startband); 2488 if (s.intensitystereo <= s.startband) 2489 totalbits += dualstereo_bit; /* no intensity stereo means no dual stereo */ 2490 else if (dualstereo_bit) 2491 s.dualstereo = opus_rc_p2model(rc, 1); 2492 2493 /* supply the remaining bits in this frame to lower bands */ 2494 remaining = totalbits - total; 2495 bandbits = remaining / (celt_freq_bands[s.codedbands] - celt_freq_bands[s.startband]); 2496 remaining -= bandbits * (celt_freq_bands[s.codedbands] - celt_freq_bands[s.startband]); 2497 for (i = s.startband; i < s.codedbands; i++) { 2498 int bits = FFMIN(remaining, celt_freq_range[i]); 2499 2500 s.pulses[i] += bits + bandbits * celt_freq_range[i]; 2501 remaining -= bits; 2502 } 2503 2504 for (i = s.startband; i < s.codedbands; i++) { 2505 int N = celt_freq_range[i] << s.duration; 2506 int prev_extra = extrabits; 2507 s.pulses[i] += extrabits; 2508 2509 if (N > 1) { 2510 int dof; // degrees of freedom 2511 int temp; // dof * channels * log(dof) 2512 int offset; // fine energy quantization offset, i.e. 2513 // extra bits assigned over the standard 2514 // totalbits/dof 2515 int fine_bits, max_bits; 2516 2517 extrabits = FFMAX(0, s.pulses[i] - cap[i]); 2518 s.pulses[i] -= extrabits; 2519 2520 /* intensity stereo makes use of an extra degree of freedom */ 2521 dof = N * s.coded_channels 2522 + (s.coded_channels == 2 && N > 2 && !s.dualstereo && i < s.intensitystereo); 2523 temp = dof * (celt_log_freq_range[i] + (s.duration<<3)); 2524 offset = (temp >> 1) - dof * CELT_FINE_OFFSET; 2525 if (N == 2) /* dof=2 is the only case that doesn't fit the model */ 2526 offset += dof<<1; 2527 2528 /* grant an additional bias for the first and second pulses */ 2529 if (s.pulses[i] + offset < 2 * (dof << 3)) 2530 offset += temp >> 2; 2531 else if (s.pulses[i] + offset < 3 * (dof << 3)) 2532 offset += temp >> 3; 2533 2534 fine_bits = (s.pulses[i] + offset + (dof << 2)) / (dof << 3); 2535 max_bits = FFMIN((s.pulses[i]>>3) >> (s.coded_channels - 1), 2536 CELT_MAX_FINE_BITS); 2537 2538 max_bits = FFMAX(max_bits, 0); 2539 2540 s.fine_bits[i] = av_clip(fine_bits, 0, max_bits); 2541 2542 /* if fine_bits was rounded down or capped, 2543 give priority for the final fine energy pass */ 2544 s.fine_priority[i] = (s.fine_bits[i] * (dof<<3) >= s.pulses[i] + offset); 2545 2546 /* the remaining bits are assigned to PVQ */ 2547 s.pulses[i] -= s.fine_bits[i] << (s.coded_channels - 1) << 3; 2548 } else { 2549 /* all bits go to fine energy except for the sign bit */ 2550 extrabits = FFMAX(0, s.pulses[i] - (s.coded_channels << 3)); 2551 s.pulses[i] -= extrabits; 2552 s.fine_bits[i] = 0; 2553 s.fine_priority[i] = 1; 2554 } 2555 2556 /* hand back a limited number of extra fine energy bits to this band */ 2557 if (extrabits > 0) { 2558 int fineextra = FFMIN(extrabits >> (s.coded_channels + 2), 2559 CELT_MAX_FINE_BITS - s.fine_bits[i]); 2560 s.fine_bits[i] += fineextra; 2561 2562 fineextra <<= s.coded_channels + 2; 2563 s.fine_priority[i] = (fineextra >= extrabits - prev_extra); 2564 extrabits -= fineextra; 2565 } 2566 } 2567 s.remaining = extrabits; 2568 2569 /* skipped bands dedicate all of their bits for fine energy */ 2570 for (; i < s.endband; i++) { 2571 s.fine_bits[i] = s.pulses[i] >> (s.coded_channels - 1) >> 3; 2572 s.pulses[i] = 0; 2573 s.fine_priority[i] = s.fine_bits[i] < 1; 2574 } 2575 } 2576 2577 /*static inline*/ int celt_bits2pulses(const(uint8_t)* cache, int bits) 2578 { 2579 // TODO: Find the size of cache and make it into an array in the parameters list 2580 int i, low = 0, high; 2581 2582 high = cache[0]; 2583 bits--; 2584 2585 for (i = 0; i < 6; i++) { 2586 int center = (low + high + 1) >> 1; 2587 if (cache[center] >= bits) 2588 high = center; 2589 else 2590 low = center; 2591 } 2592 2593 return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high; 2594 } 2595 2596 /*static inline*/ int celt_pulses2bits(const(uint8_t)* cache, int pulses) 2597 { 2598 // TODO: Find the size of cache and make it into an array in the parameters list 2599 return (pulses == 0) ? 0 : cache[pulses] + 1; 2600 } 2601 2602 /*static inline*/ void celt_normalize_residual(const(int)* /*av_restrict*/ iy, float * /*av_restrict*/ X, int N, float g) 2603 { 2604 int i; 2605 for (i = 0; i < N; i++) 2606 X[i] = g * iy[i]; 2607 } 2608 2609 private void celt_exp_rotation1(float *X, uint len, uint stride, float c, float s) 2610 { 2611 float *Xptr; 2612 int i; 2613 2614 Xptr = X; 2615 for (i = 0; i < len - stride; i++) { 2616 float x1, x2; 2617 x1 = Xptr[0]; 2618 x2 = Xptr[stride]; 2619 Xptr[stride] = c * x2 + s * x1; 2620 *Xptr++ = c * x1 - s * x2; 2621 } 2622 2623 Xptr = &X[len - 2 * stride - 1]; 2624 for (i = len - 2 * stride - 1; i >= 0; i--) { 2625 float x1, x2; 2626 x1 = Xptr[0]; 2627 x2 = Xptr[stride]; 2628 Xptr[stride] = c * x2 + s * x1; 2629 *Xptr-- = c * x1 - s * x2; 2630 } 2631 } 2632 2633 /*static inline*/ void celt_exp_rotation(float *X, uint len, uint stride, uint K, CeltSpread spread) 2634 { 2635 uint stride2 = 0; 2636 float c, s; 2637 float gain, theta; 2638 int i; 2639 2640 if (2*K >= len || spread == CELT_SPREAD_NONE) 2641 return; 2642 2643 gain = cast(float)len / (len + (20 - 5*spread) * K); 2644 theta = PI * gain * gain / 4; 2645 2646 c = cos(theta); 2647 s = sin(theta); 2648 2649 if (len >= stride << 3) { 2650 stride2 = 1; 2651 /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding. 2652 It's basically incrementing long as (stride2+0.5)^2 < len/stride. */ 2653 while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len) 2654 stride2++; 2655 } 2656 2657 /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for 2658 extract_collapse_mask().*/ 2659 len /= stride; 2660 for (i = 0; i < stride; i++) { 2661 if (stride2) 2662 celt_exp_rotation1(X + i * len, len, stride2, s, c); 2663 celt_exp_rotation1(X + i * len, len, 1, c, s); 2664 } 2665 } 2666 2667 /*static inline*/ uint celt_extract_collapse_mask(const(int)* iy, uint N, uint B) 2668 { 2669 uint collapse_mask; 2670 int N0; 2671 int i, j; 2672 2673 if (B <= 1) 2674 return 1; 2675 2676 /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for 2677 exp_rotation().*/ 2678 N0 = N/B; 2679 collapse_mask = 0; 2680 for (i = 0; i < B; i++) 2681 for (j = 0; j < N0; j++) 2682 collapse_mask |= (iy[i*N0+j]!=0)<<i; 2683 return collapse_mask; 2684 } 2685 2686 /*static inline*/ void celt_renormalize_vector(float *X, int N, float gain) 2687 { 2688 2689 int i; 2690 float g = 1e-15f; 2691 for (i = 0; i < N; i++) 2692 g += X[i] * X[i]; 2693 g = gain / sqrtf(g); 2694 2695 for (i = 0; i < N; i++) 2696 X[i] *= g; 2697 } 2698 2699 /*static inline*/ void celt_stereo_merge(float *X, float *Y, float mid, int N) 2700 { 2701 int i; 2702 float xp = 0, side = 0; 2703 float[2] E; 2704 float mid2; 2705 float t; 2706 float[2] gain; 2707 2708 /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ 2709 for (i = 0; i < N; i++) { 2710 xp += X[i] * Y[i]; 2711 side += Y[i] * Y[i]; 2712 } 2713 2714 /* Compensating for the mid normalization */ 2715 xp *= mid; 2716 mid2 = mid; 2717 E[0] = mid2 * mid2 + side - 2 * xp; 2718 E[1] = mid2 * mid2 + side + 2 * xp; 2719 if (E[0] < 6e-4f || E[1] < 6e-4f) { 2720 for (i = 0; i < N; i++) 2721 Y[i] = X[i]; 2722 return; 2723 } 2724 2725 t = E[0]; 2726 gain[0] = 1.0f / sqrtf(t); 2727 t = E[1]; 2728 gain[1] = 1.0f / sqrtf(t); 2729 2730 for (i = 0; i < N; i++) { 2731 float[2] value = void; 2732 /* Apply mid scaling (side is already scaled) */ 2733 value[0] = mid * X[i]; 2734 value[1] = Y[i]; 2735 X[i] = gain[0] * (value[0] - value[1]); 2736 Y[i] = gain[1] * (value[0] + value[1]); 2737 } 2738 } 2739 2740 private void celt_interleave_hadamard (float *tmp, float *X, int N0, int stride, int hadamard) 2741 { 2742 int i, j; 2743 int N = N0*stride; 2744 2745 if (hadamard) { 2746 const(uint8_t)* ordery = celt_hadamard_ordery.ptr + stride - 2; 2747 for (i = 0; i < stride; i++) 2748 for (j = 0; j < N0; j++) 2749 tmp[j*stride+i] = X[ordery[i]*N0+j]; 2750 } else { 2751 for (i = 0; i < stride; i++) 2752 for (j = 0; j < N0; j++) 2753 tmp[j*stride+i] = X[i*N0+j]; 2754 } 2755 2756 for (i = 0; i < N; i++) 2757 X[i] = tmp[i]; 2758 } 2759 2760 private void celt_deinterleave_hadamard (float *tmp, float *X, int N0, int stride, int hadamard) 2761 { 2762 int i, j; 2763 int N = N0*stride; 2764 2765 if (hadamard) { 2766 const(uint8_t)* ordery = celt_hadamard_ordery.ptr + stride - 2; 2767 for (i = 0; i < stride; i++) 2768 for (j = 0; j < N0; j++) 2769 tmp[ordery[i]*N0+j] = X[j*stride+i]; 2770 } else { 2771 for (i = 0; i < stride; i++) 2772 for (j = 0; j < N0; j++) 2773 tmp[i*N0+j] = X[j*stride+i]; 2774 } 2775 2776 for (i = 0; i < N; i++) 2777 X[i] = tmp[i]; 2778 } 2779 2780 private void celt_haar1(float *X, int N0, int stride) 2781 { 2782 int i, j; 2783 N0 >>= 1; 2784 for (i = 0; i < stride; i++) { 2785 for (j = 0; j < N0; j++) { 2786 float x0 = X[stride * (2 * j + 0) + i]; 2787 float x1 = X[stride * (2 * j + 1) + i]; 2788 X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2; 2789 X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2; 2790 } 2791 } 2792 } 2793 2794 /*static inline*/ int celt_compute_qn(int N, int b, int offset, int pulse_cap, int dualstereo) 2795 { 2796 int qn, qb; 2797 int N2 = 2 * N - 1; 2798 if (dualstereo && N == 2) 2799 N2--; 2800 2801 /* The upper limit ensures that in a stereo split with itheta==16384, we'll 2802 * always have enough bits left over to code at least one pulse in the 2803 * side; otherwise it would collapse, since it doesn't get folded. */ 2804 qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3); 2805 qn = (qb < (1 << 3 >> 1)) ? 1 : ((celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1; 2806 return qn; 2807 } 2808 2809 // this code was adapted from libopus 2810 /*static inline*/ uint64_t celt_cwrsi(uint N, uint K, uint i, int *y) 2811 { 2812 uint64_t norm = 0; 2813 uint32_t p; 2814 int s, val; 2815 int k0; 2816 2817 while (N > 2) { 2818 uint32_t q; 2819 2820 /*Lots of pulses case:*/ 2821 if (K >= N) { 2822 const uint32_t *row = celt_pvq_u_row[N]; 2823 2824 /* Are the pulses in this dimension negative? */ 2825 p = row[K + 1]; 2826 s = -(i >= p ? 1 : 0); 2827 i -= p & s; 2828 2829 /*Count how many pulses were placed in this dimension.*/ 2830 k0 = K; 2831 q = row[N]; 2832 if (q > i) { 2833 K = N; 2834 do { 2835 p = celt_pvq_u_row[--K][N]; 2836 } while (p > i); 2837 } else 2838 for (p = row[K]; p > i; p = row[K]) 2839 K--; 2840 2841 i -= p; 2842 val = (k0 - K + s) ^ s; 2843 norm += val * val; 2844 *y++ = val; 2845 } else { /*Lots of dimensions case:*/ 2846 /*Are there any pulses in this dimension at all?*/ 2847 p = celt_pvq_u_row[K ][N]; 2848 q = celt_pvq_u_row[K + 1][N]; 2849 2850 if (p <= i && i < q) { 2851 i -= p; 2852 *y++ = 0; 2853 } else { 2854 /*Are the pulses in this dimension negative?*/ 2855 s = -(i >= q ? 1 : 0); 2856 i -= q & s; 2857 2858 /*Count how many pulses were placed in this dimension.*/ 2859 k0 = K; 2860 do p = celt_pvq_u_row[--K][N]; 2861 while (p > i); 2862 2863 i -= p; 2864 val = (k0 - K + s) ^ s; 2865 norm += val * val; 2866 *y++ = val; 2867 } 2868 } 2869 N--; 2870 } 2871 2872 /* N == 2 */ 2873 p = 2 * K + 1; 2874 s = -(i >= p ? 1 : 0); 2875 i -= p & s; 2876 k0 = K; 2877 K = (i + 1) / 2; 2878 2879 if (K) 2880 i -= 2 * K - 1; 2881 2882 val = (k0 - K + s) ^ s; 2883 norm += val * val; 2884 *y++ = val; 2885 2886 /* N==1 */ 2887 s = -i; 2888 val = (K + s) ^ s; 2889 norm += val * val; 2890 *y = val; 2891 2892 return norm; 2893 } 2894 2895 /*static inline*/ float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint N, uint K) { 2896 uint idx; 2897 //#define CELT_PVQ_U(n, k) (celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)]) 2898 //#define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1)) 2899 enum CELT_PVQ_U(string n, string k) = "(celt_pvq_u_row[FFMIN("~n~", "~k~")][FFMAX("~n~", "~k~")])"; 2900 enum CELT_PVQ_V(string n, string k) = "("~CELT_PVQ_U!(n, k)~" + "~CELT_PVQ_U!(n, "("~k~") + 1")~")"; 2901 idx = opus_rc_unimodel(rc, mixin(CELT_PVQ_V!("N", "K"))); 2902 return celt_cwrsi(N, K, idx, y); 2903 } 2904 2905 /** Decode pulse vector and combine the result with the pitch vector to produce 2906 the final normalised signal in the current band. */ 2907 uint celt_alg_unquant(OpusRangeCoder *rc, float *X, uint N, uint K, CeltSpread spread, uint blocks, float gain) 2908 { 2909 int[176] y = void; 2910 2911 gain /= sqrtf(celt_decode_pulses(rc, y.ptr, N, K)); 2912 celt_normalize_residual(y.ptr, X, N, gain); 2913 celt_exp_rotation(X, N, blocks, K, spread); 2914 return celt_extract_collapse_mask(y.ptr, N, blocks); 2915 } 2916 2917 int celt_decode_band(CeltContext *s, OpusRangeCoder *rc, 2918 const int band, float *X, float *Y, 2919 int N, int b, uint blocks, 2920 float *lowband, int duration, 2921 float *lowband_out, int level, float gain, float *lowband_scratch, int fill) 2922 { 2923 const(uint8_t)* cache; 2924 int dualstereo, split; 2925 int imid = 0, iside = 0; 2926 uint N0 = N; 2927 int N_B; 2928 int N_B0; 2929 int B0 = blocks; 2930 int time_divide = 0; 2931 int recombine = 0; 2932 int inv = 0; 2933 float mid = 0, side = 0; 2934 int longblocks = (B0 == 1); 2935 uint cm = 0; 2936 2937 N_B0 = N_B = N / blocks; 2938 split = dualstereo = (Y !is null); 2939 2940 if (N == 1) { 2941 /* special case for one sample */ 2942 int i; 2943 float *x = X; 2944 for (i = 0; i <= dualstereo; i++) { 2945 int sign = 0; 2946 if (s.remaining2 >= 1<<3) { 2947 sign = opus_getrawbits(rc, 1); 2948 s.remaining2 -= 1 << 3; 2949 b -= 1 << 3; 2950 } 2951 x[0] = sign ? -1.0f : 1.0f; 2952 x = Y; 2953 } 2954 if (lowband_out) 2955 lowband_out[0] = X[0]; 2956 return 1; 2957 } 2958 2959 if (!dualstereo && level == 0) { 2960 int tf_change = s.tf_change[band]; 2961 int k; 2962 if (tf_change > 0) 2963 recombine = tf_change; 2964 /* Band recombining to increase frequency resolution */ 2965 2966 if (lowband && 2967 (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) { 2968 int j; 2969 for (j = 0; j < N; j++) 2970 lowband_scratch[j] = lowband[j]; 2971 lowband = lowband_scratch; 2972 } 2973 2974 for (k = 0; k < recombine; k++) { 2975 if (lowband) 2976 celt_haar1(lowband, N >> k, 1 << k); 2977 fill = celt_bit_interleave[fill & 0xF] | celt_bit_interleave[fill >> 4] << 2; 2978 } 2979 blocks >>= recombine; 2980 N_B <<= recombine; 2981 2982 /* Increasing the time resolution */ 2983 while ((N_B & 1) == 0 && tf_change < 0) { 2984 if (lowband) 2985 celt_haar1(lowband, N_B, blocks); 2986 fill |= fill << blocks; 2987 blocks <<= 1; 2988 N_B >>= 1; 2989 time_divide++; 2990 tf_change++; 2991 } 2992 B0 = blocks; 2993 N_B0 = N_B; 2994 2995 /* Reorganize the samples in time order instead of frequency order */ 2996 if (B0 > 1 && lowband) 2997 celt_deinterleave_hadamard(s.scratch.ptr, lowband, N_B >> recombine, B0 << recombine, longblocks); 2998 } 2999 3000 /* If we need 1.5 more bit than we can produce, split the band in two. */ 3001 cache = celt_cache_bits.ptr + celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band]; 3002 if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) { 3003 N >>= 1; 3004 Y = X + N; 3005 split = 1; 3006 duration -= 1; 3007 if (blocks == 1) 3008 fill = (fill & 1) | (fill << 1); 3009 blocks = (blocks + 1) >> 1; 3010 } 3011 3012 if (split) { 3013 int qn; 3014 int itheta = 0; 3015 int mbits, sbits, delta; 3016 int qalloc; 3017 int pulse_cap; 3018 int offset; 3019 int orig_fill; 3020 int tell; 3021 3022 /* Decide on the resolution to give to the split parameter theta */ 3023 pulse_cap = celt_log_freq_range[band] + duration * 8; 3024 offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE : 3025 CELT_QTHETA_OFFSET); 3026 qn = (dualstereo && band >= s.intensitystereo) ? 1 : 3027 celt_compute_qn(N, b, offset, pulse_cap, dualstereo); 3028 tell = opus_rc_tell_frac(rc); 3029 if (qn != 1) { 3030 /* Entropy coding of the angle. We use a uniform pdf for the 3031 time split, a step for stereo, and a triangular one for the rest. */ 3032 if (dualstereo && N > 2) 3033 itheta = opus_rc_stepmodel(rc, qn/2); 3034 else if (dualstereo || B0 > 1) 3035 itheta = opus_rc_unimodel(rc, qn+1); 3036 else 3037 itheta = opus_rc_trimodel(rc, qn); 3038 itheta = itheta * 16384 / qn; 3039 /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. 3040 Let's do that at higher complexity */ 3041 } else if (dualstereo) { 3042 inv = (b > 2 << 3 && s.remaining2 > 2 << 3) ? opus_rc_p2model(rc, 2) : 0; 3043 itheta = 0; 3044 } 3045 qalloc = opus_rc_tell_frac(rc) - tell; 3046 b -= qalloc; 3047 3048 orig_fill = fill; 3049 if (itheta == 0) { 3050 imid = 32767; 3051 iside = 0; 3052 fill = av_mod_uintp2(fill, blocks); 3053 delta = -16384; 3054 } else if (itheta == 16384) { 3055 imid = 0; 3056 iside = 32767; 3057 fill &= ((1 << blocks) - 1) << blocks; 3058 delta = 16384; 3059 } else { 3060 imid = celt_cos(cast(short)itheta); 3061 iside = celt_cos(cast(short)(16384-itheta)); 3062 /* This is the mid vs side allocation that minimizes squared error 3063 in that band. */ 3064 delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid)); 3065 } 3066 3067 mid = imid / 32768.0f; 3068 side = iside / 32768.0f; 3069 3070 /* This is a special case for N=2 that only works for stereo and takes 3071 advantage of the fact that mid and side are orthogonal to encode 3072 the side with just one bit. */ 3073 if (N == 2 && dualstereo) { 3074 int c; 3075 int sign = 0; 3076 float tmp; 3077 float* x2, y2; 3078 mbits = b; 3079 /* Only need one bit for the side */ 3080 sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0; 3081 mbits -= sbits; 3082 c = (itheta > 8192); 3083 s.remaining2 -= qalloc+sbits; 3084 3085 x2 = c ? Y : X; 3086 y2 = c ? X : Y; 3087 if (sbits) 3088 sign = opus_getrawbits(rc, 1); 3089 sign = 1 - 2 * sign; 3090 /* We use orig_fill here because we want to fold the side, but if 3091 itheta==16384, we'll have cleared the low bits of fill. */ 3092 cm = celt_decode_band(s, rc, band, x2, null, N, mbits, blocks, 3093 lowband, duration, lowband_out, level, gain, 3094 lowband_scratch, orig_fill); 3095 /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), 3096 and there's no need to worry about mixing with the other channel. */ 3097 y2[0] = -sign * x2[1]; 3098 y2[1] = sign * x2[0]; 3099 X[0] *= mid; 3100 X[1] *= mid; 3101 Y[0] *= side; 3102 Y[1] *= side; 3103 tmp = X[0]; 3104 X[0] = tmp - Y[0]; 3105 Y[0] = tmp + Y[0]; 3106 tmp = X[1]; 3107 X[1] = tmp - Y[1]; 3108 Y[1] = tmp + Y[1]; 3109 } else { 3110 /* "Normal" split code */ 3111 float *next_lowband2 = null; 3112 float *next_lowband_out1 = null; 3113 int next_level = 0; 3114 int rebalance; 3115 3116 /* Give more bits to low-energy MDCTs than they would 3117 * otherwise deserve */ 3118 if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) { 3119 if (itheta > 8192) 3120 /* Rough approximation for pre-echo masking */ 3121 delta -= delta >> (4 - duration); 3122 else 3123 /* Corresponds to a forward-masking slope of 3124 * 1.5 dB per 10 ms */ 3125 delta = FFMIN(0, delta + (N << 3 >> (5 - duration))); 3126 } 3127 mbits = av_clip((b - delta) / 2, 0, b); 3128 sbits = b - mbits; 3129 s.remaining2 -= qalloc; 3130 3131 if (lowband && !dualstereo) 3132 next_lowband2 = lowband + N; /* >32-bit split case */ 3133 3134 /* Only stereo needs to pass on lowband_out. 3135 * Otherwise, it's handled at the end */ 3136 if (dualstereo) 3137 next_lowband_out1 = lowband_out; 3138 else 3139 next_level = level + 1; 3140 3141 rebalance = s.remaining2; 3142 if (mbits >= sbits) { 3143 /* In stereo mode, we do not apply a scaling to the mid 3144 * because we need the normalized mid for folding later */ 3145 cm = celt_decode_band(s, rc, band, X, null, N, mbits, blocks, 3146 lowband, duration, next_lowband_out1, 3147 next_level, dualstereo ? 1.0f : (gain * mid), 3148 lowband_scratch, fill); 3149 3150 rebalance = mbits - (rebalance - s.remaining2); 3151 if (rebalance > 3 << 3 && itheta != 0) 3152 sbits += rebalance - (3 << 3); 3153 3154 /* For a stereo split, the high bits of fill are always zero, 3155 * so no folding will be done to the side. */ 3156 cm |= celt_decode_band(s, rc, band, Y, null, N, sbits, blocks, 3157 next_lowband2, duration, null, 3158 next_level, gain * side, null, 3159 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1)); 3160 } else { 3161 /* For a stereo split, the high bits of fill are always zero, 3162 * so no folding will be done to the side. */ 3163 cm = celt_decode_band(s, rc, band, Y, null, N, sbits, blocks, 3164 next_lowband2, duration, null, 3165 next_level, gain * side, null, 3166 fill >> blocks) << ((B0 >> 1) & (dualstereo - 1)); 3167 3168 rebalance = sbits - (rebalance - s.remaining2); 3169 if (rebalance > 3 << 3 && itheta != 16384) 3170 mbits += rebalance - (3 << 3); 3171 3172 /* In stereo mode, we do not apply a scaling to the mid because 3173 * we need the normalized mid for folding later */ 3174 cm |= celt_decode_band(s, rc, band, X, null, N, mbits, blocks, 3175 lowband, duration, next_lowband_out1, 3176 next_level, dualstereo ? 1.0f : (gain * mid), 3177 lowband_scratch, fill); 3178 } 3179 } 3180 } else { 3181 /* This is the basic no-split case */ 3182 uint q = celt_bits2pulses(cache, b); 3183 uint curr_bits = celt_pulses2bits(cache, q); 3184 s.remaining2 -= curr_bits; 3185 3186 /* Ensures we can never bust the budget */ 3187 while (s.remaining2 < 0 && q > 0) { 3188 s.remaining2 += curr_bits; 3189 curr_bits = celt_pulses2bits(cache, --q); 3190 s.remaining2 -= curr_bits; 3191 } 3192 3193 if (q != 0) { 3194 /* Finally do the actual quantization */ 3195 cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1), 3196 s.spread, blocks, gain); 3197 } else { 3198 /* If there's no pulse, fill the band anyway */ 3199 int j; 3200 uint cm_mask = (1 << blocks) - 1; 3201 fill &= cm_mask; 3202 if (!fill) { 3203 for (j = 0; j < N; j++) 3204 X[j] = 0.0f; 3205 } else { 3206 if (!lowband) { 3207 /* Noise */ 3208 for (j = 0; j < N; j++) 3209 X[j] = ((cast(int32_t)celt_rng(s)) >> 20); 3210 cm = cm_mask; 3211 } else { 3212 /* Folded spectrum */ 3213 for (j = 0; j < N; j++) { 3214 /* About 48 dB below the "normal" folding level */ 3215 X[j] = lowband[j] + (((celt_rng(s)) & 0x8000) ? 1.0f / 256 : -1.0f / 256); 3216 } 3217 cm = fill; 3218 } 3219 celt_renormalize_vector(X, N, gain); 3220 } 3221 } 3222 } 3223 3224 /* This code is used by the decoder and by the resynthesis-enabled encoder */ 3225 if (dualstereo) { 3226 int j; 3227 if (N != 2) 3228 celt_stereo_merge(X, Y, mid, N); 3229 if (inv) { 3230 for (j = 0; j < N; j++) 3231 Y[j] *= -1; 3232 } 3233 } else if (level == 0) { 3234 int k; 3235 3236 /* Undo the sample reorganization going from time order to frequency order */ 3237 if (B0 > 1) 3238 celt_interleave_hadamard(s.scratch.ptr, X, N_B>>recombine, B0<<recombine, longblocks); 3239 3240 /* Undo time-freq changes that we did earlier */ 3241 N_B = N_B0; 3242 blocks = B0; 3243 for (k = 0; k < time_divide; k++) { 3244 blocks >>= 1; 3245 N_B <<= 1; 3246 cm |= cm >> blocks; 3247 celt_haar1(X, N_B, blocks); 3248 } 3249 3250 for (k = 0; k < recombine; k++) { 3251 cm = celt_bit_deinterleave[cm]; 3252 celt_haar1(X, N0>>k, 1<<k); 3253 } 3254 blocks <<= recombine; 3255 3256 /* Scale output for later folding */ 3257 if (lowband_out) { 3258 int j; 3259 float n = sqrtf(N0); 3260 for (j = 0; j < N0; j++) 3261 lowband_out[j] = n * X[j]; 3262 } 3263 cm = av_mod_uintp2(cm, blocks); 3264 } 3265 return cm; 3266 } 3267 3268 private void celt_denormalize(CeltContext *s, CeltFrame *frame, float *data) 3269 { 3270 int i, j; 3271 3272 for (i = s.startband; i < s.endband; i++) { 3273 float *dst = data + (celt_freq_bands[i] << s.duration); 3274 float norm = exp2(frame.energy[i] + celt_mean_energy[i]); 3275 3276 for (j = 0; j < celt_freq_range[i] << s.duration; j++) 3277 dst[j] *= norm; 3278 } 3279 } 3280 3281 private void celt_postfilter_apply_transition(CeltFrame *frame, float *data) 3282 { 3283 const int T0 = frame.pf_period_old; 3284 const int T1 = frame.pf_period; 3285 3286 float g00, g01, g02; 3287 float g10, g11, g12; 3288 3289 float x0, x1, x2, x3, x4; 3290 3291 int i; 3292 3293 if (frame.pf_gains[0] == 0.0 && 3294 frame.pf_gains_old[0] == 0.0) 3295 return; 3296 3297 g00 = frame.pf_gains_old[0]; 3298 g01 = frame.pf_gains_old[1]; 3299 g02 = frame.pf_gains_old[2]; 3300 g10 = frame.pf_gains[0]; 3301 g11 = frame.pf_gains[1]; 3302 g12 = frame.pf_gains[2]; 3303 3304 x1 = data[-T1 + 1]; 3305 x2 = data[-T1]; 3306 x3 = data[-T1 - 1]; 3307 x4 = data[-T1 - 2]; 3308 3309 for (i = 0; i < CELT_OVERLAP; i++) { 3310 float w = ff_celt_window2[i]; 3311 x0 = data[i - T1 + 2]; 3312 3313 data[i] += (1.0 - w) * g00 * data[i - T0] + 3314 (1.0 - w) * g01 * (data[i - T0 - 1] + data[i - T0 + 1]) + 3315 (1.0 - w) * g02 * (data[i - T0 - 2] + data[i - T0 + 2]) + 3316 w * g10 * x2 + 3317 w * g11 * (x1 + x3) + 3318 w * g12 * (x0 + x4); 3319 x4 = x3; 3320 x3 = x2; 3321 x2 = x1; 3322 x1 = x0; 3323 } 3324 } 3325 3326 private void celt_postfilter_apply(CeltFrame *frame, float *data, int len) 3327 { 3328 const int T = frame.pf_period; 3329 float g0, g1, g2; 3330 float x0, x1, x2, x3, x4; 3331 int i; 3332 3333 if (frame.pf_gains[0] == 0.0 || len <= 0) 3334 return; 3335 3336 g0 = frame.pf_gains[0]; 3337 g1 = frame.pf_gains[1]; 3338 g2 = frame.pf_gains[2]; 3339 3340 x4 = data[-T - 2]; 3341 x3 = data[-T - 1]; 3342 x2 = data[-T]; 3343 x1 = data[-T + 1]; 3344 3345 for (i = 0; i < len; i++) { 3346 x0 = data[i - T + 2]; 3347 data[i] += g0 * x2 + 3348 g1 * (x1 + x3) + 3349 g2 * (x0 + x4); 3350 x4 = x3; 3351 x3 = x2; 3352 x2 = x1; 3353 x1 = x0; 3354 } 3355 } 3356 3357 private void celt_postfilter(CeltContext *s, CeltFrame *frame) 3358 { 3359 int len = s.blocksize * s.blocks; 3360 3361 celt_postfilter_apply_transition(frame, frame.buf.ptr + 1024); 3362 3363 frame.pf_period_old = frame.pf_period; 3364 memcpy(frame.pf_gains_old.ptr, frame.pf_gains.ptr, frame.pf_gains.sizeof); 3365 3366 frame.pf_period = frame.pf_period_new; 3367 memcpy(frame.pf_gains.ptr, frame.pf_gains_new.ptr, frame.pf_gains.sizeof); 3368 3369 if (len > CELT_OVERLAP) { 3370 celt_postfilter_apply_transition(frame, frame.buf.ptr + 1024 + CELT_OVERLAP); 3371 celt_postfilter_apply(frame, frame.buf.ptr + 1024 + 2 * CELT_OVERLAP, len - 2 * CELT_OVERLAP); 3372 3373 frame.pf_period_old = frame.pf_period; 3374 memcpy(frame.pf_gains_old.ptr, frame.pf_gains.ptr, frame.pf_gains.sizeof); 3375 } 3376 3377 memmove(frame.buf.ptr, frame.buf.ptr + len, (1024 + CELT_OVERLAP / 2) * float.sizeof); 3378 } 3379 3380 private int parse_postfilter(CeltContext *s, OpusRangeCoder *rc, int consumed) 3381 { 3382 static immutable float[3][3] postfilter_taps = [ 3383 [ 0.3066406250f, 0.2170410156f, 0.1296386719f ], 3384 [ 0.4638671875f, 0.2680664062f, 0.0 ], 3385 [ 0.7998046875f, 0.1000976562f, 0.0 ], 3386 ]; 3387 int i; 3388 3389 memset(s.frame[0].pf_gains_new.ptr, 0, (s.frame[0].pf_gains_new).sizeof); 3390 memset(s.frame[1].pf_gains_new.ptr, 0, (s.frame[1].pf_gains_new).sizeof); 3391 3392 if (s.startband == 0 && consumed + 16 <= s.framebits) { 3393 int has_postfilter = opus_rc_p2model(rc, 1); 3394 if (has_postfilter) { 3395 float gain; 3396 int tapset, octave, period; 3397 3398 octave = opus_rc_unimodel(rc, 6); 3399 period = (16 << octave) + opus_getrawbits(rc, 4 + octave) - 1; 3400 gain = 0.09375f * (opus_getrawbits(rc, 3) + 1); 3401 tapset = (opus_rc_tell(rc) + 2 <= s.framebits) ? 3402 opus_rc_getsymbol(rc, celt_model_tapset.ptr) : 0; 3403 3404 for (i = 0; i < 2; i++) { 3405 CeltFrame *frame = &s.frame[i]; 3406 3407 frame.pf_period_new = FFMAX(period, CELT_POSTFILTER_MINPERIOD); 3408 frame.pf_gains_new[0] = gain * postfilter_taps[tapset][0]; 3409 frame.pf_gains_new[1] = gain * postfilter_taps[tapset][1]; 3410 frame.pf_gains_new[2] = gain * postfilter_taps[tapset][2]; 3411 } 3412 } 3413 3414 consumed = opus_rc_tell(rc); 3415 } 3416 3417 return consumed; 3418 } 3419 3420 private void process_anticollapse(CeltContext *s, CeltFrame *frame, float *X) 3421 { 3422 int i, j, k; 3423 3424 for (i = s.startband; i < s.endband; i++) { 3425 int renormalize = 0; 3426 float *xptr; 3427 float[2] prev; 3428 float Ediff, r; 3429 float thresh, sqrt_1; 3430 int depth; 3431 3432 /* depth in 1/8 bits */ 3433 depth = (1 + s.pulses[i]) / (celt_freq_range[i] << s.duration); 3434 thresh = exp2f(-1.0 - 0.125f * depth); 3435 sqrt_1 = 1.0f / sqrtf(celt_freq_range[i] << s.duration); 3436 3437 xptr = X + (celt_freq_bands[i] << s.duration); 3438 3439 prev[0] = frame.prev_energy[0][i]; 3440 prev[1] = frame.prev_energy[1][i]; 3441 if (s.coded_channels == 1) { 3442 CeltFrame *frame1 = &s.frame[1]; 3443 3444 prev[0] = FFMAX(prev[0], frame1.prev_energy[0][i]); 3445 prev[1] = FFMAX(prev[1], frame1.prev_energy[1][i]); 3446 } 3447 Ediff = frame.energy[i] - FFMIN(prev[0], prev[1]); 3448 Ediff = FFMAX(0, Ediff); 3449 3450 /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because 3451 short blocks don't have the same energy as long */ 3452 r = exp2(1 - Ediff); 3453 if (s.duration == 3) 3454 r *= M_SQRT2; 3455 r = FFMIN(thresh, r) * sqrt_1; 3456 for (k = 0; k < 1 << s.duration; k++) { 3457 /* Detect collapse */ 3458 if (!(frame.collapse_masks[i] & 1 << k)) { 3459 /* Fill with noise */ 3460 for (j = 0; j < celt_freq_range[i]; j++) 3461 xptr[(j << s.duration) + k] = (celt_rng(s) & 0x8000) ? r : -r; 3462 renormalize = 1; 3463 } 3464 } 3465 3466 /* We just added some energy, so we need to renormalize */ 3467 if (renormalize) 3468 celt_renormalize_vector(xptr, celt_freq_range[i] << s.duration, 1.0f); 3469 } 3470 } 3471 3472 private void celt_decode_bands(CeltContext *s, OpusRangeCoder *rc) 3473 { 3474 float[8 * 22] lowband_scratch = void; 3475 float[2 * 8 * 100] norm = void; 3476 3477 int totalbits = (s.framebits << 3) - s.anticollapse_bit; 3478 3479 int update_lowband = 1; 3480 int lowband_offset = 0; 3481 3482 int i, j; 3483 3484 memset(s.coeffs.ptr, 0, s.coeffs.sizeof); 3485 3486 for (i = s.startband; i < s.endband; i++) { 3487 int band_offset = celt_freq_bands[i] << s.duration; 3488 int band_size = celt_freq_range[i] << s.duration; 3489 float *X = s.coeffs[0].ptr + band_offset; 3490 float *Y = (s.coded_channels == 2) ? s.coeffs[1].ptr + band_offset : null; 3491 3492 int consumed = opus_rc_tell_frac(rc); 3493 float *norm2 = norm.ptr + 8 * 100; 3494 int effective_lowband = -1; 3495 uint[2] cm; 3496 int b; 3497 3498 /* Compute how many bits we want to allocate to this band */ 3499 if (i != s.startband) 3500 s.remaining -= consumed; 3501 s.remaining2 = totalbits - consumed - 1; 3502 if (i <= s.codedbands - 1) { 3503 int curr_balance = s.remaining / FFMIN(3, s.codedbands-i); 3504 b = av_clip_uintp2(FFMIN(s.remaining2 + 1, s.pulses[i] + curr_balance), 14); 3505 } else 3506 b = 0; 3507 3508 if (celt_freq_bands[i] - celt_freq_range[i] >= celt_freq_bands[s.startband] && 3509 (update_lowband || lowband_offset == 0)) 3510 lowband_offset = i; 3511 3512 /* Get a conservative estimate of the collapse_mask's for the bands we're 3513 going to be folding from. */ 3514 if (lowband_offset != 0 && (s.spread != CELT_SPREAD_AGGRESSIVE || 3515 s.blocks > 1 || s.tf_change[i] < 0)) { 3516 int foldstart, foldend; 3517 3518 /* This ensures we never repeat spectral content within one band */ 3519 effective_lowband = FFMAX(celt_freq_bands[s.startband], 3520 celt_freq_bands[lowband_offset] - celt_freq_range[i]); 3521 foldstart = lowband_offset; 3522 while (celt_freq_bands[--foldstart] > effective_lowband) {} 3523 foldend = lowband_offset - 1; 3524 while (celt_freq_bands[++foldend] < effective_lowband + celt_freq_range[i]) {} 3525 3526 cm[0] = cm[1] = 0; 3527 for (j = foldstart; j < foldend; j++) { 3528 cm[0] |= s.frame[0].collapse_masks[j]; 3529 cm[1] |= s.frame[s.coded_channels - 1].collapse_masks[j]; 3530 } 3531 } else 3532 /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost 3533 always) be non-zero.*/ 3534 cm[0] = cm[1] = (1 << s.blocks) - 1; 3535 3536 if (s.dualstereo && i == s.intensitystereo) { 3537 /* Switch off dual stereo to do intensity */ 3538 s.dualstereo = 0; 3539 for (j = celt_freq_bands[s.startband] << s.duration; j < band_offset; j++) 3540 norm[j] = (norm[j] + norm2[j]) / 2; 3541 } 3542 3543 if (s.dualstereo) { 3544 cm[0] = celt_decode_band(s, rc, i, X, null, band_size, b / 2, s.blocks, 3545 effective_lowband != -1 ? norm.ptr + (effective_lowband << s.duration) : null, s.duration, 3546 norm.ptr + band_offset, 0, 1.0f, lowband_scratch.ptr, cm[0]); 3547 3548 cm[1] = celt_decode_band(s, rc, i, Y, null, band_size, b/2, s.blocks, 3549 effective_lowband != -1 ? norm2 + (effective_lowband << s.duration) : null, s.duration, 3550 norm2 + band_offset, 0, 1.0f, lowband_scratch.ptr, cm[1]); 3551 } else { 3552 cm[0] = celt_decode_band(s, rc, i, X, Y, band_size, b, s.blocks, 3553 effective_lowband != -1 ? norm.ptr + (effective_lowband << s.duration) : null, s.duration, 3554 norm.ptr + band_offset, 0, 1.0f, lowband_scratch.ptr, cm[0]|cm[1]); 3555 3556 cm[1] = cm[0]; 3557 } 3558 3559 s.frame[0].collapse_masks[i] = cast(uint8_t)cm[0]; 3560 s.frame[s.coded_channels - 1].collapse_masks[i] = cast(uint8_t)cm[1]; 3561 s.remaining += s.pulses[i] + consumed; 3562 3563 /* Update the folding position only as long as we have 1 bit/sample depth */ 3564 update_lowband = (b > band_size << 3); 3565 } 3566 } 3567 3568 int ff_celt_decode_frame(CeltContext *s, OpusRangeCoder *rc, 3569 float **output, int coded_channels, int frame_size, 3570 int startband, int endband) 3571 { 3572 int i, j; 3573 3574 int consumed; // bits of entropy consumed thus far for this frame 3575 int silence = 0; 3576 int transient = 0; 3577 int anticollapse = 0; 3578 IMDCT15Context *imdct; 3579 float imdct_scale = 1.0; 3580 3581 if (coded_channels != 1 && coded_channels != 2) { 3582 //av_log(AV_LOG_ERROR, "Invalid number of coded channels: %d\n", coded_channels); 3583 return AVERROR_INVALIDDATA; 3584 } 3585 if (startband < 0 || startband > endband || endband > CELT_MAX_BANDS) { 3586 //av_log(AV_LOG_ERROR, "Invalid start/end band: %d %d\n", startband, endband); 3587 return AVERROR_INVALIDDATA; 3588 } 3589 3590 s.flushed = 0; 3591 s.coded_channels = coded_channels; 3592 s.startband = startband; 3593 s.endband = endband; 3594 s.framebits = rc.rb.bytes * 8; 3595 3596 s.duration = av_log2(frame_size / CELT_SHORT_BLOCKSIZE); 3597 if (s.duration > CELT_MAX_LOG_BLOCKS || 3598 frame_size != CELT_SHORT_BLOCKSIZE * (1 << s.duration)) { 3599 //av_log(AV_LOG_ERROR, "Invalid CELT frame size: %d\n", frame_size); 3600 return AVERROR_INVALIDDATA; 3601 } 3602 3603 if (!s.output_channels) 3604 s.output_channels = coded_channels; 3605 3606 memset(s.frame[0].collapse_masks.ptr, 0, s.frame[0].collapse_masks.sizeof); 3607 memset(s.frame[1].collapse_masks.ptr, 0, s.frame[1].collapse_masks.sizeof); 3608 3609 consumed = opus_rc_tell(rc); 3610 3611 /* obtain silence flag */ 3612 if (consumed >= s.framebits) 3613 silence = 1; 3614 else if (consumed == 1) 3615 silence = opus_rc_p2model(rc, 15); 3616 3617 3618 if (silence) { 3619 consumed = s.framebits; 3620 rc.total_read_bits += s.framebits - opus_rc_tell(rc); 3621 } 3622 3623 /* obtain post-filter options */ 3624 consumed = parse_postfilter(s, rc, consumed); 3625 3626 /* obtain transient flag */ 3627 if (s.duration != 0 && consumed+3 <= s.framebits) 3628 transient = opus_rc_p2model(rc, 3); 3629 3630 s.blocks = transient ? 1 << s.duration : 1; 3631 s.blocksize = frame_size / s.blocks; 3632 3633 imdct = s.imdct[transient ? 0 : s.duration]; 3634 3635 if (coded_channels == 1) { 3636 for (i = 0; i < CELT_MAX_BANDS; i++) 3637 s.frame[0].energy[i] = FFMAX(s.frame[0].energy[i], s.frame[1].energy[i]); 3638 } 3639 3640 celt_decode_coarse_energy(s, rc); 3641 celt_decode_tf_changes (s, rc, transient); 3642 celt_decode_allocation (s, rc); 3643 celt_decode_fine_energy (s, rc); 3644 celt_decode_bands (s, rc); 3645 3646 if (s.anticollapse_bit) 3647 anticollapse = opus_getrawbits(rc, 1); 3648 3649 celt_decode_final_energy(s, rc, s.framebits - opus_rc_tell(rc)); 3650 3651 /* apply anti-collapse processing and denormalization to 3652 * each coded channel */ 3653 for (i = 0; i < s.coded_channels; i++) { 3654 CeltFrame *frame = &s.frame[i]; 3655 3656 if (anticollapse) 3657 process_anticollapse(s, frame, s.coeffs[i].ptr); 3658 3659 celt_denormalize(s, frame, s.coeffs[i].ptr); 3660 } 3661 3662 /* stereo . mono downmix */ 3663 if (s.output_channels < s.coded_channels) { 3664 vector_fmac_scalar(s.coeffs[0].ptr, s.coeffs[1].ptr, 1.0f, /*FFALIGN(frame_size, 16)*/frame_size); 3665 imdct_scale = 0.5; 3666 } else if (s.output_channels > s.coded_channels) 3667 memcpy(s.coeffs[1].ptr, s.coeffs[0].ptr, frame_size * float.sizeof); 3668 3669 if (silence) { 3670 for (i = 0; i < 2; i++) { 3671 CeltFrame *frame = &s.frame[i]; 3672 3673 for (j = 0; j < /*FF_ARRAY_ELEMS*/frame.energy.length; j++) 3674 frame.energy[j] = CELT_ENERGY_SILENCE; 3675 } 3676 memset(s.coeffs.ptr, 0, s.coeffs.sizeof); 3677 } 3678 3679 /* transform and output for each output channel */ 3680 for (i = 0; i < s.output_channels; i++) { 3681 CeltFrame *frame = &s.frame[i]; 3682 float m = frame.deemph_coeff; 3683 3684 /* iMDCT and overlap-add */ 3685 for (j = 0; j < s.blocks; j++) { 3686 float *dst = frame.buf.ptr + 1024 + j * s.blocksize; 3687 3688 imdct.imdct_half(imdct, dst + CELT_OVERLAP / 2, s.coeffs[i].ptr + j, s.blocks, imdct_scale); 3689 vector_fmul_window(dst, dst, dst + CELT_OVERLAP / 2, celt_window.ptr, CELT_OVERLAP / 2); 3690 } 3691 3692 /* postfilter */ 3693 celt_postfilter(s, frame); 3694 3695 /* deemphasis and output scaling */ 3696 for (j = 0; j < frame_size; j++) { 3697 float tmp = frame.buf[1024 - frame_size + j] + m; 3698 m = tmp * CELT_DEEMPH_COEFF; 3699 output[i][j] = tmp / 32768.; 3700 } 3701 frame.deemph_coeff = m; 3702 } 3703 3704 if (coded_channels == 1) 3705 memcpy(s.frame[1].energy.ptr, s.frame[0].energy.ptr, s.frame[0].energy.sizeof); 3706 3707 for (i = 0; i < 2; i++ ) { 3708 CeltFrame *frame = &s.frame[i]; 3709 3710 if (!transient) { 3711 memcpy(frame.prev_energy[1].ptr, frame.prev_energy[0].ptr, frame.prev_energy[0].sizeof); 3712 memcpy(frame.prev_energy[0].ptr, frame.energy.ptr, frame.prev_energy[0].sizeof); 3713 } else { 3714 for (j = 0; j < CELT_MAX_BANDS; j++) 3715 frame.prev_energy[0][j] = FFMIN(frame.prev_energy[0][j], frame.energy[j]); 3716 } 3717 3718 for (j = 0; j < s.startband; j++) { 3719 frame.prev_energy[0][j] = CELT_ENERGY_SILENCE; 3720 frame.energy[j] = 0.0; 3721 } 3722 for (j = s.endband; j < CELT_MAX_BANDS; j++) { 3723 frame.prev_energy[0][j] = CELT_ENERGY_SILENCE; 3724 frame.energy[j] = 0.0; 3725 } 3726 } 3727 3728 s.seed = rc.range; 3729 3730 return 0; 3731 } 3732 3733 void ff_celt_flush(CeltContext *s) 3734 { 3735 int i, j; 3736 3737 if (s.flushed) 3738 return; 3739 3740 for (i = 0; i < 2; i++) { 3741 CeltFrame *frame = &s.frame[i]; 3742 3743 for (j = 0; j < CELT_MAX_BANDS; j++) 3744 frame.prev_energy[0][j] = frame.prev_energy[1][j] = CELT_ENERGY_SILENCE; 3745 3746 memset(frame.energy.ptr, 0, frame.energy.sizeof); 3747 memset(frame.buf.ptr, 0, frame.buf.sizeof); 3748 3749 memset(frame.pf_gains.ptr, 0, frame.pf_gains.sizeof); 3750 memset(frame.pf_gains_old.ptr, 0, frame.pf_gains_old.sizeof); 3751 memset(frame.pf_gains_new.ptr, 0, frame.pf_gains_new.sizeof); 3752 3753 frame.deemph_coeff = 0.0; 3754 } 3755 s.seed = 0; 3756 3757 s.flushed = 1; 3758 } 3759 3760 void ff_celt_free(CeltContext **ps) 3761 { 3762 CeltContext *s = *ps; 3763 int i; 3764 3765 if (!s) 3766 return; 3767 3768 for (i = 0; i < /*FF_ARRAY_ELEMS*/s.imdct.length; i++) 3769 ff_imdct15_uninit(&s.imdct[i]); 3770 3771 //av_freep(&s.dsp); 3772 av_freep(ps); 3773 } 3774 3775 int ff_celt_init(/*AVCodecContext *avctx,*/ CeltContext **ps, int output_channels) 3776 { 3777 CeltContext *s; 3778 int i, ret; 3779 3780 if (output_channels != 1 && output_channels != 2) { 3781 //av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", output_channels); 3782 return AVERROR(EINVAL); 3783 } 3784 3785 s = av_mallocz!CeltContext(); 3786 if (!s) 3787 return AVERROR(ENOMEM); 3788 3789 //s.avctx = avctx; 3790 s.output_channels = output_channels; 3791 3792 for (i = 0; i < /*FF_ARRAY_ELEMS*/s.imdct.length; i++) { 3793 ret = ff_imdct15_init(&s.imdct[i], i + 3); 3794 if (ret < 0) 3795 goto fail; 3796 } 3797 3798 //!!!s.dsp = avpriv_float_dsp_alloc(avctx.flags & AV_CODEC_FLAG_BITEXACT); 3799 /*if (!s.dsp) { 3800 ret = AVERROR(ENOMEM); 3801 goto fail; 3802 }*/ 3803 3804 ff_celt_flush(s); 3805 3806 *ps = s; 3807 3808 return 0; 3809 fail: 3810 ff_celt_free(&s); 3811 return ret; 3812 } 3813 3814 3815 struct SilkFrame { 3816 int coded; 3817 int log_gain; 3818 int16_t[16] nlsf; 3819 float[16] lpc; 3820 3821 float[2 * SILK_HISTORY] output; 3822 float[2 * SILK_HISTORY] lpc_history; 3823 int primarylag; 3824 3825 int prev_voiced; 3826 } 3827 3828 struct SilkContext { 3829 //AVCodecContext *avctx; 3830 int output_channels; 3831 3832 int midonly; 3833 int subframes; 3834 int sflength; 3835 int flength; 3836 int nlsf_interp_factor; 3837 3838 OpusBandwidth bandwidth; 3839 int wb; 3840 3841 SilkFrame[2] frame; 3842 float[2] prev_stereo_weights; 3843 float[2] stereo_weights; 3844 3845 int prev_coded_channels; 3846 } 3847 3848 static immutable uint16_t[26] silk_model_stereo_s1 = [ 3849 256, 7, 9, 10, 11, 12, 22, 46, 54, 55, 56, 59, 82, 174, 197, 200, 3850 201, 202, 210, 234, 244, 245, 246, 247, 249, 256 3851 ]; 3852 3853 static immutable uint16_t[4] silk_model_stereo_s2 = [256, 85, 171, 256]; 3854 3855 static immutable uint16_t[6] silk_model_stereo_s3 = [256, 51, 102, 154, 205, 256]; 3856 3857 static immutable uint16_t[3] silk_model_mid_only = [256, 192, 256]; 3858 3859 static immutable uint16_t[3] silk_model_frame_type_inactive = [256, 26, 256]; 3860 3861 static immutable uint16_t[5] silk_model_frame_type_active = [256, 24, 98, 246, 256]; 3862 3863 static immutable uint16_t[9][3] silk_model_gain_highbits = [ 3864 [256, 32, 144, 212, 241, 253, 254, 255, 256], 3865 [256, 2, 19, 64, 124, 186, 233, 252, 256], 3866 [256, 1, 4, 30, 101, 195, 245, 254, 256] 3867 ]; 3868 3869 static immutable uint16_t[9] silk_model_gain_lowbits = [256, 32, 64, 96, 128, 160, 192, 224, 256]; 3870 3871 static immutable uint16_t[42] silk_model_gain_delta = [ 3872 256, 6, 11, 22, 53, 185, 206, 214, 218, 221, 223, 225, 227, 228, 229, 230, 3873 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 3874 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 3875 ]; 3876 static immutable uint16_t[33][2][2] silk_model_lsf_s1 = [ 3877 [ 3878 [ // NB or MB, unvoiced 3879 256, 44, 78, 108, 127, 148, 160, 171, 174, 177, 179, 195, 197, 199, 200, 205, 3880 207, 208, 211, 214, 215, 216, 218, 220, 222, 225, 226, 235, 244, 246, 253, 255, 256 3881 ], [ // NB or MB, voiced 3882 256, 1, 11, 12, 20, 23, 31, 39, 53, 66, 80, 81, 95, 107, 120, 131, 3883 142, 154, 165, 175, 185, 196, 204, 213, 221, 228, 236, 237, 238, 244, 245, 251, 256 3884 ] 3885 ], [ 3886 [ // WB, unvoiced 3887 256, 31, 52, 55, 72, 73, 81, 98, 102, 103, 121, 137, 141, 143, 146, 147, 3888 157, 158, 161, 177, 188, 204, 206, 208, 211, 213, 224, 225, 229, 238, 246, 253, 256 3889 ], [ // WB, voiced 3890 256, 1, 5, 21, 26, 44, 55, 60, 74, 89, 90, 93, 105, 118, 132, 146, 3891 152, 166, 178, 180, 186, 187, 199, 211, 222, 232, 235, 245, 250, 251, 252, 253, 256 3892 ] 3893 ] 3894 ]; 3895 3896 static immutable uint16_t[10][32] silk_model_lsf_s2 = [ 3897 // NB, MB 3898 [ 256, 1, 2, 3, 18, 242, 253, 254, 255, 256 ], 3899 [ 256, 1, 2, 4, 38, 221, 253, 254, 255, 256 ], 3900 [ 256, 1, 2, 6, 48, 197, 252, 254, 255, 256 ], 3901 [ 256, 1, 2, 10, 62, 185, 246, 254, 255, 256 ], 3902 [ 256, 1, 4, 20, 73, 174, 248, 254, 255, 256 ], 3903 [ 256, 1, 4, 21, 76, 166, 239, 254, 255, 256 ], 3904 [ 256, 1, 8, 32, 85, 159, 226, 252, 255, 256 ], 3905 [ 256, 1, 2, 20, 83, 161, 219, 249, 255, 256 ], 3906 3907 // WB 3908 [ 256, 1, 2, 3, 12, 244, 253, 254, 255, 256 ], 3909 [ 256, 1, 2, 4, 32, 218, 253, 254, 255, 256 ], 3910 [ 256, 1, 2, 5, 47, 199, 252, 254, 255, 256 ], 3911 [ 256, 1, 2, 12, 61, 187, 252, 254, 255, 256 ], 3912 [ 256, 1, 5, 24, 72, 172, 249, 254, 255, 256 ], 3913 [ 256, 1, 2, 16, 70, 170, 242, 254, 255, 256 ], 3914 [ 256, 1, 2, 17, 78, 165, 226, 251, 255, 256 ], 3915 [ 256, 1, 8, 29, 79, 156, 237, 254, 255, 256 ] 3916 ]; 3917 3918 static immutable uint16_t[8] silk_model_lsf_s2_ext = [ 256, 156, 216, 240, 249, 253, 255, 256 ]; 3919 3920 static immutable uint16_t[6] silk_model_lsf_interpolation_offset = [ 256, 13, 35, 64, 75, 256 ]; 3921 3922 static immutable uint16_t[33] silk_model_pitch_highbits = [ 3923 256, 3, 6, 12, 23, 44, 74, 106, 125, 136, 146, 158, 171, 184, 196, 207, 3924 216, 224, 231, 237, 241, 243, 245, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 3925 ]; 3926 3927 static immutable uint16_t[5] silk_model_pitch_lowbits_nb= [ 256, 64, 128, 192, 256 ]; 3928 3929 static immutable uint16_t[7] silk_model_pitch_lowbits_mb= [ 256, 43, 85, 128, 171, 213, 256 ]; 3930 3931 static immutable uint16_t[9] silk_model_pitch_lowbits_wb= [ 256, 32, 64, 96, 128, 160, 192, 224, 256 ]; 3932 3933 static immutable uint16_t[22] silk_model_pitch_delta = [ 3934 256, 46, 48, 50, 53, 57, 63, 73, 88, 114, 152, 182, 204, 219, 229, 236, 3935 242, 246, 250, 252, 254, 256 3936 ]; 3937 3938 static immutable uint16_t[4] silk_model_pitch_contour_nb10ms = [ 256, 143, 193, 256 ]; 3939 3940 static immutable uint16_t[12] silk_model_pitch_contour_nb20ms = [ 3941 256, 68, 80, 101, 118, 137, 159, 189, 213, 230, 246, 256 3942 ]; 3943 3944 static immutable uint16_t[13] silk_model_pitch_contour_mbwb10ms = [ 3945 256, 91, 137, 176, 195, 209, 221, 229, 236, 242, 247, 252, 256 3946 ]; 3947 3948 static immutable uint16_t[35] silk_model_pitch_contour_mbwb20ms = [ 3949 256, 33, 55, 73, 89, 104, 118, 132, 145, 158, 168, 177, 186, 194, 200, 206, 3950 212, 217, 221, 225, 229, 232, 235, 238, 240, 242, 244, 246, 248, 250, 252, 253, 3951 254, 255, 256 3952 ]; 3953 3954 static immutable uint16_t[4] silk_model_ltp_filter = [ 256, 77, 157, 256 ]; 3955 3956 static immutable uint16_t[9] silk_model_ltp_filter0_sel = [ 3957 256, 185, 200, 213, 226, 235, 244, 250, 256 3958 ]; 3959 3960 static immutable uint16_t[17] silk_model_ltp_filter1_sel = [ 3961 256, 57, 91, 112, 132, 147, 160, 172, 185, 195, 205, 214, 224, 233, 241, 248, 256 3962 ]; 3963 3964 static immutable uint16_t[33] silk_model_ltp_filter2_sel = [ 3965 256, 15, 31, 45, 57, 69, 81, 92, 103, 114, 124, 133, 142, 151, 160, 168, 3966 176, 184, 192, 199, 206, 212, 218, 223, 227, 232, 236, 240, 244, 247, 251, 254, 256 3967 ]; 3968 3969 static immutable uint16_t[4] silk_model_ltp_scale_index = [ 256, 128, 192, 256 ]; 3970 3971 static immutable uint16_t[5] silk_model_lcg_seed = [ 256, 64, 128, 192, 256 ]; 3972 3973 static immutable uint16_t[10][2] silk_model_exc_rate = [ 3974 [ 256, 15, 66, 78, 124, 169, 182, 215, 242, 256 ], // unvoiced 3975 [ 256, 33, 63, 99, 116, 150, 199, 217, 238, 256 ] // voiced 3976 ]; 3977 3978 static immutable uint16_t[19][11] silk_model_pulse_count = [ 3979 [ 256, 131, 205, 230, 238, 241, 244, 245, 246, 3980 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 ], 3981 [ 256, 58, 151, 211, 234, 241, 244, 245, 246, 3982 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 ], 3983 [ 256, 43, 94, 140, 173, 197, 213, 224, 232, 3984 238, 241, 244, 247, 249, 250, 251, 253, 254, 256 ], 3985 [ 256, 17, 69, 140, 197, 228, 240, 245, 246, 3986 247, 248, 249, 250, 251, 252, 253, 254, 255, 256 ], 3987 [ 256, 6, 27, 68, 121, 170, 205, 226, 237, 3988 243, 246, 248, 250, 251, 252, 253, 254, 255, 256 ], 3989 [ 256, 7, 21, 43, 71, 100, 128, 153, 173, 3990 190, 203, 214, 223, 230, 235, 239, 243, 246, 256 ], 3991 [ 256, 2, 7, 21, 50, 92, 138, 179, 210, 3992 229, 240, 246, 249, 251, 252, 253, 254, 255, 256 ], 3993 [ 256, 1, 3, 7, 17, 36, 65, 100, 137, 3994 171, 199, 219, 233, 241, 246, 250, 252, 254, 256 ], 3995 [ 256, 1, 3, 5, 10, 19, 33, 53, 77, 3996 104, 132, 158, 181, 201, 216, 227, 235, 241, 256 ], 3997 [ 256, 1, 2, 3, 9, 36, 94, 150, 189, 3998 214, 228, 238, 244, 247, 250, 252, 253, 254, 256 ], 3999 [ 256, 2, 3, 9, 36, 94, 150, 189, 214, 4000 228, 238, 244, 247, 250, 252, 253, 254, 256, 256 ] 4001 ]; 4002 4003 static immutable uint16_t[168][4] silk_model_pulse_location = [ 4004 [ 4005 256, 126, 256, 4006 256, 56, 198, 256, 4007 256, 25, 126, 230, 256, 4008 256, 12, 72, 180, 244, 256, 4009 256, 7, 42, 126, 213, 250, 256, 4010 256, 4, 24, 83, 169, 232, 253, 256, 4011 256, 3, 15, 53, 125, 200, 242, 254, 256, 4012 256, 2, 10, 35, 89, 162, 221, 248, 255, 256, 4013 256, 2, 7, 24, 63, 126, 191, 233, 251, 255, 256, 4014 256, 1, 5, 17, 45, 94, 157, 211, 241, 252, 255, 256, 4015 256, 1, 5, 13, 33, 70, 125, 182, 223, 245, 253, 255, 256, 4016 256, 1, 4, 11, 26, 54, 98, 151, 199, 232, 248, 254, 255, 256, 4017 256, 1, 3, 9, 21, 42, 77, 124, 172, 212, 237, 249, 254, 255, 256, 4018 256, 1, 2, 6, 16, 33, 60, 97, 144, 187, 220, 241, 250, 254, 255, 256, 4019 256, 1, 2, 3, 11, 25, 47, 80, 120, 163, 201, 229, 245, 253, 254, 255, 256, 4020 256, 1, 2, 3, 4, 17, 35, 62, 98, 139, 180, 214, 238, 252, 253, 254, 255, 256 4021 ],[ 4022 256, 127, 256, 4023 256, 53, 202, 256, 4024 256, 22, 127, 233, 256, 4025 256, 11, 72, 183, 246, 256, 4026 256, 6, 41, 127, 215, 251, 256, 4027 256, 4, 24, 83, 170, 232, 253, 256, 4028 256, 3, 16, 56, 127, 200, 241, 254, 256, 4029 256, 3, 12, 39, 92, 162, 218, 246, 255, 256, 4030 256, 3, 11, 30, 67, 124, 185, 229, 249, 255, 256, 4031 256, 3, 10, 25, 53, 97, 151, 200, 233, 250, 255, 256, 4032 256, 1, 8, 21, 43, 77, 123, 171, 209, 237, 251, 255, 256, 4033 256, 1, 2, 13, 35, 62, 97, 139, 186, 219, 244, 254, 255, 256, 4034 256, 1, 2, 8, 22, 48, 85, 128, 171, 208, 234, 248, 254, 255, 256, 4035 256, 1, 2, 6, 16, 36, 67, 107, 149, 189, 220, 240, 250, 254, 255, 256, 4036 256, 1, 2, 5, 13, 29, 55, 90, 128, 166, 201, 227, 243, 251, 254, 255, 256, 4037 256, 1, 2, 4, 10, 22, 43, 73, 109, 147, 183, 213, 234, 246, 252, 254, 255, 256 4038 ],[ 4039 256, 127, 256, 4040 256, 49, 206, 256, 4041 256, 20, 127, 236, 256, 4042 256, 11, 71, 184, 246, 256, 4043 256, 7, 43, 127, 214, 250, 256, 4044 256, 6, 30, 87, 169, 229, 252, 256, 4045 256, 5, 23, 62, 126, 194, 236, 252, 256, 4046 256, 6, 20, 49, 96, 157, 209, 239, 253, 256, 4047 256, 1, 16, 39, 74, 125, 175, 215, 245, 255, 256, 4048 256, 1, 2, 23, 55, 97, 149, 195, 236, 254, 255, 256, 4049 256, 1, 7, 23, 50, 86, 128, 170, 206, 233, 249, 255, 256, 4050 256, 1, 6, 18, 39, 70, 108, 148, 186, 217, 238, 250, 255, 256, 4051 256, 1, 4, 13, 30, 56, 90, 128, 166, 200, 226, 243, 252, 255, 256, 4052 256, 1, 4, 11, 25, 47, 76, 110, 146, 180, 209, 231, 245, 252, 255, 256, 4053 256, 1, 3, 8, 19, 37, 62, 93, 128, 163, 194, 219, 237, 248, 253, 255, 256, 4054 256, 1, 2, 6, 15, 30, 51, 79, 111, 145, 177, 205, 226, 241, 250, 254, 255, 256 4055 ],[ 4056 256, 128, 256, 4057 256, 42, 214, 256, 4058 256, 21, 128, 235, 256, 4059 256, 12, 72, 184, 245, 256, 4060 256, 8, 42, 128, 214, 249, 256, 4061 256, 8, 31, 86, 176, 231, 251, 256, 4062 256, 5, 20, 58, 130, 202, 238, 253, 256, 4063 256, 6, 18, 45, 97, 174, 221, 241, 251, 256, 4064 256, 6, 25, 53, 88, 128, 168, 203, 231, 250, 256, 4065 256, 4, 18, 40, 71, 108, 148, 185, 216, 238, 252, 256, 4066 256, 3, 13, 31, 57, 90, 128, 166, 199, 225, 243, 253, 256, 4067 256, 2, 10, 23, 44, 73, 109, 147, 183, 212, 233, 246, 254, 256, 4068 256, 1, 6, 16, 33, 58, 90, 128, 166, 198, 223, 240, 250, 255, 256, 4069 256, 1, 5, 12, 25, 46, 75, 110, 146, 181, 210, 231, 244, 251, 255, 256, 4070 256, 1, 3, 8, 18, 35, 60, 92, 128, 164, 196, 221, 238, 248, 253, 255, 256, 4071 256, 1, 3, 7, 14, 27, 48, 76, 110, 146, 180, 208, 229, 242, 249, 253, 255, 256 4072 ] 4073 ]; 4074 4075 static immutable uint16_t[3] silk_model_excitation_lsb = [256, 136, 256]; 4076 4077 static immutable uint16_t[3][7][2][3] silk_model_excitation_sign = [ 4078 [ // Inactive 4079 [ // Low offset 4080 [256, 2, 256], 4081 [256, 207, 256], 4082 [256, 189, 256], 4083 [256, 179, 256], 4084 [256, 174, 256], 4085 [256, 163, 256], 4086 [256, 157, 256] 4087 ], [ // High offset 4088 [256, 58, 256], 4089 [256, 245, 256], 4090 [256, 238, 256], 4091 [256, 232, 256], 4092 [256, 225, 256], 4093 [256, 220, 256], 4094 [256, 211, 256] 4095 ] 4096 ], [ // Unvoiced 4097 [ // Low offset 4098 [256, 1, 256], 4099 [256, 210, 256], 4100 [256, 190, 256], 4101 [256, 178, 256], 4102 [256, 169, 256], 4103 [256, 162, 256], 4104 [256, 152, 256] 4105 ], [ // High offset 4106 [256, 48, 256], 4107 [256, 242, 256], 4108 [256, 235, 256], 4109 [256, 224, 256], 4110 [256, 214, 256], 4111 [256, 205, 256], 4112 [256, 190, 256] 4113 ] 4114 ], [ // Voiced 4115 [ // Low offset 4116 [256, 1, 256], 4117 [256, 162, 256], 4118 [256, 152, 256], 4119 [256, 147, 256], 4120 [256, 144, 256], 4121 [256, 141, 256], 4122 [256, 138, 256] 4123 ], [ // High offset 4124 [256, 8, 256], 4125 [256, 203, 256], 4126 [256, 187, 256], 4127 [256, 176, 256], 4128 [256, 168, 256], 4129 [256, 161, 256], 4130 [256, 154, 256] 4131 ] 4132 ] 4133 ]; 4134 4135 static immutable int16_t[16] silk_stereo_weights = [ 4136 -13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, 4137 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732 4138 ]; 4139 4140 static immutable uint8_t[10][32] silk_lsf_s2_model_sel_nbmb = [ 4141 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4142 [ 1, 3, 1, 2, 2, 1, 2, 1, 1, 1 ], 4143 [ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 ], 4144 [ 1, 2, 2, 2, 2, 1, 2, 1, 1, 1 ], 4145 [ 2, 3, 3, 3, 3, 2, 2, 2, 2, 2 ], 4146 [ 0, 5, 3, 3, 2, 2, 2, 2, 1, 1 ], 4147 [ 0, 2, 2, 2, 2, 2, 2, 2, 2, 1 ], 4148 [ 2, 3, 6, 4, 4, 4, 5, 4, 5, 5 ], 4149 [ 2, 4, 5, 5, 4, 5, 4, 6, 4, 4 ], 4150 [ 2, 4, 4, 7, 4, 5, 4, 5, 5, 4 ], 4151 [ 4, 3, 3, 3, 2, 3, 2, 2, 2, 2 ], 4152 [ 1, 5, 5, 6, 4, 5, 4, 5, 5, 5 ], 4153 [ 2, 7, 4, 6, 5, 5, 5, 5, 5, 5 ], 4154 [ 2, 7, 5, 5, 5, 5, 5, 6, 5, 4 ], 4155 [ 3, 3, 5, 4, 4, 5, 4, 5, 4, 4 ], 4156 [ 2, 3, 3, 5, 5, 4, 4, 4, 4, 4 ], 4157 [ 2, 4, 4, 6, 4, 5, 4, 5, 5, 5 ], 4158 [ 2, 5, 4, 6, 5, 5, 5, 4, 5, 4 ], 4159 [ 2, 7, 4, 5, 4, 5, 4, 5, 5, 5 ], 4160 [ 2, 5, 4, 6, 7, 6, 5, 6, 5, 4 ], 4161 [ 3, 6, 7, 4, 6, 5, 5, 6, 4, 5 ], 4162 [ 2, 7, 6, 4, 4, 4, 5, 4, 5, 5 ], 4163 [ 4, 5, 5, 4, 6, 6, 5, 6, 5, 4 ], 4164 [ 2, 5, 5, 6, 5, 6, 4, 6, 4, 4 ], 4165 [ 4, 5, 5, 5, 3, 7, 4, 5, 5, 4 ], 4166 [ 2, 3, 4, 5, 5, 6, 4, 5, 5, 4 ], 4167 [ 2, 3, 2, 3, 3, 4, 2, 3, 3, 3 ], 4168 [ 1, 1, 2, 2, 2, 2, 2, 3, 2, 2 ], 4169 [ 4, 5, 5, 6, 6, 6, 5, 6, 4, 5 ], 4170 [ 3, 5, 5, 4, 4, 4, 4, 3, 3, 2 ], 4171 [ 2, 5, 3, 7, 5, 5, 4, 4, 5, 4 ], 4172 [ 4, 4, 5, 4, 5, 6, 5, 6, 5, 4 ] 4173 ]; 4174 4175 static immutable uint8_t[16][32] silk_lsf_s2_model_sel_wb = [ 4176 [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 ], 4177 [ 10, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 9, 9, 9, 8, 11 ], 4178 [ 10, 13, 13, 11, 15, 12, 12, 13, 10, 13, 12, 13, 13, 12, 11, 11 ], 4179 [ 8, 10, 9, 10, 10, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 9 ], 4180 [ 8, 14, 13, 12, 14, 12, 15, 13, 12, 12, 12, 13, 13, 12, 12, 11 ], 4181 [ 8, 11, 13, 13, 12, 11, 11, 13, 11, 11, 11, 11, 11, 11, 10, 12 ], 4182 [ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 ], 4183 [ 8, 10, 14, 11, 15, 10, 13, 11, 12, 13, 13, 12, 11, 11, 10, 11 ], 4184 [ 8, 14, 10, 14, 14, 12, 13, 12, 14, 13, 12, 12, 13, 11, 11, 11 ], 4185 [ 10, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 ], 4186 [ 8, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9 ], 4187 [ 10, 10, 11, 12, 13, 11, 11, 11, 11, 11, 11, 11, 10, 10, 9, 11 ], 4188 [ 10, 10, 11, 11, 12, 11, 11, 11, 11, 11, 11, 11, 11, 10, 9, 11 ], 4189 [ 11, 12, 12, 12, 14, 12, 12, 13, 11, 13, 12, 12, 13, 12, 11, 12 ], 4190 [ 8, 14, 12, 13, 12, 15, 13, 10, 14, 13, 15, 12, 12, 11, 13, 11 ], 4191 [ 8, 9, 8, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 9, 8 ], 4192 [ 9, 14, 13, 15, 13, 12, 13, 11, 12, 13, 12, 12, 12, 11, 11, 12 ], 4193 [ 9, 11, 11, 12, 12, 11, 11, 13, 10, 11, 11, 13, 13, 13, 11, 12 ], 4194 [ 10, 11, 11, 10, 10, 10, 11, 10, 9, 10, 9, 10, 9, 9, 9, 12 ], 4195 [ 8, 10, 11, 13, 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 8 ], 4196 [ 11, 12, 11, 13, 11, 11, 10, 10, 9, 9, 9, 9, 9, 10, 10, 12 ], 4197 [ 10, 14, 11, 15, 15, 12, 13, 12, 13, 11, 13, 11, 11, 10, 11, 11 ], 4198 [ 10, 11, 13, 14, 14, 11, 13, 11, 12, 12, 11, 11, 11, 11, 10, 12 ], 4199 [ 9, 11, 11, 12, 12, 12, 12, 11, 13, 13, 13, 11, 9, 9, 9, 9 ], 4200 [ 10, 13, 11, 14, 14, 12, 15, 12, 12, 13, 11, 12, 12, 11, 11, 11 ], 4201 [ 8, 14, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 ], 4202 [ 8, 14, 14, 11, 13, 10, 13, 13, 11, 12, 12, 15, 15, 12, 12, 12 ], 4203 [ 11, 11, 15, 11, 13, 12, 11, 11, 11, 10, 10, 11, 11, 11, 10, 11 ], 4204 [ 8, 8, 9, 8, 8, 8, 10, 9, 10, 9, 9, 10, 10, 10, 9, 9 ], 4205 [ 8, 11, 10, 13, 11, 11, 10, 11, 10, 9, 8, 8, 9, 8, 8, 9 ], 4206 [ 11, 13, 13, 12, 15, 13, 11, 11, 10, 11, 10, 10, 9, 8, 9, 8 ], 4207 [ 10, 11, 13, 11, 12, 11, 11, 11, 10, 9, 10, 14, 12, 8, 8, 8 ] 4208 ]; 4209 4210 static immutable uint8_t[9][2] silk_lsf_pred_weights_nbmb = [ 4211 [179, 138, 140, 148, 151, 149, 153, 151, 163], 4212 [116, 67, 82, 59, 92, 72, 100, 89, 92] 4213 ]; 4214 4215 static immutable uint8_t[15][2] silk_lsf_pred_weights_wb = [ 4216 [175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182], 4217 [ 68, 62, 66, 60, 72, 117, 85, 90, 118, 136, 151, 142, 160, 142, 155] 4218 ]; 4219 4220 static immutable uint8_t[9][32] silk_lsf_weight_sel_nbmb = [ 4221 [ 0, 1, 0, 0, 0, 0, 0, 0, 0 ], 4222 [ 1, 0, 0, 0, 0, 0, 0, 0, 0 ], 4223 [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4224 [ 1, 1, 1, 0, 0, 0, 0, 1, 0 ], 4225 [ 0, 1, 0, 0, 0, 0, 0, 0, 0 ], 4226 [ 0, 1, 0, 0, 0, 0, 0, 0, 0 ], 4227 [ 1, 0, 1, 1, 0, 0, 0, 1, 0 ], 4228 [ 0, 1, 1, 0, 0, 1, 1, 0, 0 ], 4229 [ 0, 0, 1, 1, 0, 1, 0, 1, 1 ], 4230 [ 0, 0, 1, 1, 0, 0, 1, 1, 1 ], 4231 [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4232 [ 0, 1, 0, 1, 1, 1, 1, 1, 0 ], 4233 [ 0, 1, 0, 1, 1, 1, 1, 1, 0 ], 4234 [ 0, 1, 1, 1, 1, 1, 1, 1, 0 ], 4235 [ 1, 0, 1, 1, 0, 1, 1, 1, 1 ], 4236 [ 0, 1, 1, 1, 1, 1, 0, 1, 0 ], 4237 [ 0, 0, 1, 1, 0, 1, 0, 1, 0 ], 4238 [ 0, 0, 1, 1, 1, 0, 1, 1, 1 ], 4239 [ 0, 1, 1, 0, 0, 1, 1, 1, 0 ], 4240 [ 0, 0, 0, 1, 1, 1, 0, 1, 0 ], 4241 [ 0, 1, 1, 0, 0, 1, 0, 1, 0 ], 4242 [ 0, 1, 1, 0, 0, 0, 1, 1, 0 ], 4243 [ 0, 0, 0, 0, 0, 1, 1, 1, 1 ], 4244 [ 0, 0, 1, 1, 0, 0, 0, 1, 1 ], 4245 [ 0, 0, 0, 1, 0, 1, 1, 1, 1 ], 4246 [ 0, 1, 1, 1, 1, 1, 1, 1, 0 ], 4247 [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4248 [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4249 [ 0, 0, 1, 0, 1, 1, 0, 1, 0 ], 4250 [ 1, 0, 0, 1, 0, 0, 0, 0, 0 ], 4251 [ 0, 0, 0, 1, 1, 0, 1, 0, 1 ], 4252 [ 1, 0, 1, 1, 0, 1, 1, 1, 1 ] 4253 ]; 4254 4255 static immutable uint8_t[15][32] silk_lsf_weight_sel_wb = [ 4256 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], 4257 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4258 [ 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0 ], 4259 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 ], 4260 [ 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0 ], 4261 [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4262 [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 ], 4263 [ 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1 ], 4264 [ 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1 ], 4265 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], 4266 [ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4267 [ 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0 ], 4268 [ 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 ], 4269 [ 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0 ], 4270 [ 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1 ], 4271 [ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 ], 4272 [ 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0 ], 4273 [ 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0 ], 4274 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], 4275 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 ], 4276 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], 4277 [ 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0 ], 4278 [ 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0 ], 4279 [ 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 ], 4280 [ 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1 ], 4281 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], 4282 [ 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1 ], 4283 [ 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ], 4284 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], 4285 [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ], 4286 [ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 ], 4287 [ 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0 ] 4288 ]; 4289 4290 static immutable uint8_t[10][32] silk_lsf_codebook_nbmb = [ 4291 [ 12, 35, 60, 83, 108, 132, 157, 180, 206, 228 ], 4292 [ 15, 32, 55, 77, 101, 125, 151, 175, 201, 225 ], 4293 [ 19, 42, 66, 89, 114, 137, 162, 184, 209, 230 ], 4294 [ 12, 25, 50, 72, 97, 120, 147, 172, 200, 223 ], 4295 [ 26, 44, 69, 90, 114, 135, 159, 180, 205, 225 ], 4296 [ 13, 22, 53, 80, 106, 130, 156, 180, 205, 228 ], 4297 [ 15, 25, 44, 64, 90, 115, 142, 168, 196, 222 ], 4298 [ 19, 24, 62, 82, 100, 120, 145, 168, 190, 214 ], 4299 [ 22, 31, 50, 79, 103, 120, 151, 170, 203, 227 ], 4300 [ 21, 29, 45, 65, 106, 124, 150, 171, 196, 224 ], 4301 [ 30, 49, 75, 97, 121, 142, 165, 186, 209, 229 ], 4302 [ 19, 25, 52, 70, 93, 116, 143, 166, 192, 219 ], 4303 [ 26, 34, 62, 75, 97, 118, 145, 167, 194, 217 ], 4304 [ 25, 33, 56, 70, 91, 113, 143, 165, 196, 223 ], 4305 [ 21, 34, 51, 72, 97, 117, 145, 171, 196, 222 ], 4306 [ 20, 29, 50, 67, 90, 117, 144, 168, 197, 221 ], 4307 [ 22, 31, 48, 66, 95, 117, 146, 168, 196, 222 ], 4308 [ 24, 33, 51, 77, 116, 134, 158, 180, 200, 224 ], 4309 [ 21, 28, 70, 87, 106, 124, 149, 170, 194, 217 ], 4310 [ 26, 33, 53, 64, 83, 117, 152, 173, 204, 225 ], 4311 [ 27, 34, 65, 95, 108, 129, 155, 174, 210, 225 ], 4312 [ 20, 26, 72, 99, 113, 131, 154, 176, 200, 219 ], 4313 [ 34, 43, 61, 78, 93, 114, 155, 177, 205, 229 ], 4314 [ 23, 29, 54, 97, 124, 138, 163, 179, 209, 229 ], 4315 [ 30, 38, 56, 89, 118, 129, 158, 178, 200, 231 ], 4316 [ 21, 29, 49, 63, 85, 111, 142, 163, 193, 222 ], 4317 [ 27, 48, 77, 103, 133, 158, 179, 196, 215, 232 ], 4318 [ 29, 47, 74, 99, 124, 151, 176, 198, 220, 237 ], 4319 [ 33, 42, 61, 76, 93, 121, 155, 174, 207, 225 ], 4320 [ 29, 53, 87, 112, 136, 154, 170, 188, 208, 227 ], 4321 [ 24, 30, 52, 84, 131, 150, 166, 186, 203, 229 ], 4322 [ 37, 48, 64, 84, 104, 118, 156, 177, 201, 230 ] 4323 ]; 4324 4325 static immutable uint8_t[16][32] silk_lsf_codebook_wb = [ 4326 [ 7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239 ], 4327 [ 13, 25, 41, 55, 69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236 ], 4328 [ 15, 21, 34, 51, 61, 78, 92, 106, 126, 136, 152, 167, 185, 205, 225, 240 ], 4329 [ 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173, 189, 205, 221, 237 ], 4330 [ 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240 ], 4331 [ 10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236 ], 4332 [ 8, 21, 37, 51, 65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218 ], 4333 [ 12, 15, 34, 55, 63, 78, 87, 108, 118, 131, 148, 167, 185, 203, 219, 236 ], 4334 [ 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171, 186, 204, 220, 237 ], 4335 [ 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241 ], 4336 [ 6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225 ], 4337 [ 11, 19, 30, 44, 57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234 ], 4338 [ 12, 19, 29, 46, 57, 71, 88, 100, 120, 132, 148, 165, 182, 199, 216, 233 ], 4339 [ 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167, 185, 204, 222, 237 ], 4340 [ 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240 ], 4341 [ 9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237 ], 4342 [ 16, 19, 36, 48, 57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236 ], 4343 [ 12, 17, 29, 54, 71, 81, 94, 104, 126, 136, 149, 164, 182, 201, 221, 237 ], 4344 [ 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180, 194, 208, 223, 238 ], 4345 [ 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239 ], 4346 [ 17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235 ], 4347 [ 14, 19, 36, 45, 61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238 ], 4348 [ 12, 18, 31, 45, 60, 76, 91, 107, 123, 138, 154, 171, 187, 204, 221, 236 ], 4349 [ 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167, 185, 203, 220, 237 ], 4350 [ 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240 ], 4351 [ 8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239 ], 4352 [ 13, 16, 41, 66, 73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241 ], 4353 [ 17, 25, 37, 52, 63, 75, 92, 102, 119, 132, 144, 160, 175, 191, 212, 231 ], 4354 [ 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187, 200, 213, 227, 242 ], 4355 [ 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239 ], 4356 [ 16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240 ], 4357 [ 15, 21, 35, 50, 61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237 ] 4358 ]; 4359 4360 static immutable uint16_t[11] silk_lsf_min_spacing_nbmb = [ 4361 250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461 4362 ]; 4363 4364 static immutable uint16_t[17] silk_lsf_min_spacing_wb = [ 4365 100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347 4366 ]; 4367 4368 static immutable uint8_t[10] silk_lsf_ordering_nbmb = [ 4369 0, 9, 6, 3, 4, 5, 8, 1, 2, 7 4370 ]; 4371 4372 static immutable uint8_t[16] silk_lsf_ordering_wb = [ 4373 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1 4374 ]; 4375 4376 static immutable int16_t[129] silk_cosine = [ /* (0.12) */ 4377 4096, 4095, 4091, 4085, 4378 4076, 4065, 4052, 4036, 4379 4017, 3997, 3973, 3948, 4380 3920, 3889, 3857, 3822, 4381 3784, 3745, 3703, 3659, 4382 3613, 3564, 3513, 3461, 4383 3406, 3349, 3290, 3229, 4384 3166, 3102, 3035, 2967, 4385 2896, 2824, 2751, 2676, 4386 2599, 2520, 2440, 2359, 4387 2276, 2191, 2106, 2019, 4388 1931, 1842, 1751, 1660, 4389 1568, 1474, 1380, 1285, 4390 1189, 1093, 995, 897, 4391 799, 700, 601, 501, 4392 401, 301, 201, 101, 4393 0, -101, -201, -301, 4394 -401, -501, -601, -700, 4395 -799, -897, -995, -1093, 4396 -1189, -1285, -1380, -1474, 4397 -1568, -1660, -1751, -1842, 4398 -1931, -2019, -2106, -2191, 4399 -2276, -2359, -2440, -2520, 4400 -2599, -2676, -2751, -2824, 4401 -2896, -2967, -3035, -3102, 4402 -3166, -3229, -3290, -3349, 4403 -3406, -3461, -3513, -3564, 4404 -3613, -3659, -3703, -3745, 4405 -3784, -3822, -3857, -3889, 4406 -3920, -3948, -3973, -3997, 4407 -4017, -4036, -4052, -4065, 4408 -4076, -4085, -4091, -4095, 4409 -4096 4410 ]; 4411 4412 static immutable uint16_t[3] silk_pitch_scale = [ 4, 6, 8]; 4413 4414 static immutable uint16_t[3] silk_pitch_min_lag = [ 16, 24, 32]; 4415 4416 static immutable uint16_t[3] silk_pitch_max_lag = [144, 216, 288]; 4417 4418 static immutable int8_t[2][3] silk_pitch_offset_nb10ms = [ 4419 [ 0, 0], 4420 [ 1, 0], 4421 [ 0, 1] 4422 ]; 4423 4424 static immutable int8_t[4][11] silk_pitch_offset_nb20ms = [ 4425 [ 0, 0, 0, 0], 4426 [ 2, 1, 0, -1], 4427 [-1, 0, 1, 2], 4428 [-1, 0, 0, 1], 4429 [-1, 0, 0, 0], 4430 [ 0, 0, 0, 1], 4431 [ 0, 0, 1, 1], 4432 [ 1, 1, 0, 0], 4433 [ 1, 0, 0, 0], 4434 [ 0, 0, 0, -1], 4435 [ 1, 0, 0, -1] 4436 ]; 4437 4438 static immutable int8_t[2][12] silk_pitch_offset_mbwb10ms = [ 4439 [ 0, 0], 4440 [ 0, 1], 4441 [ 1, 0], 4442 [-1, 1], 4443 [ 1, -1], 4444 [-1, 2], 4445 [ 2, -1], 4446 [-2, 2], 4447 [ 2, -2], 4448 [-2, 3], 4449 [ 3, -2], 4450 [-3, 3] 4451 ]; 4452 4453 static immutable int8_t[4][34] silk_pitch_offset_mbwb20ms = [ 4454 [ 0, 0, 0, 0], 4455 [ 0, 0, 1, 1], 4456 [ 1, 1, 0, 0], 4457 [-1, 0, 0, 0], 4458 [ 0, 0, 0, 1], 4459 [ 1, 0, 0, 0], 4460 [-1, 0, 0, 1], 4461 [ 0, 0, 0, -1], 4462 [-1, 0, 1, 2], 4463 [ 1, 0, 0, -1], 4464 [-2, -1, 1, 2], 4465 [ 2, 1, 0, -1], 4466 [-2, 0, 0, 2], 4467 [-2, 0, 1, 3], 4468 [ 2, 1, -1, -2], 4469 [-3, -1, 1, 3], 4470 [ 2, 0, 0, -2], 4471 [ 3, 1, 0, -2], 4472 [-3, -1, 2, 4], 4473 [-4, -1, 1, 4], 4474 [ 3, 1, -1, -3], 4475 [-4, -1, 2, 5], 4476 [ 4, 2, -1, -3], 4477 [ 4, 1, -1, -4], 4478 [-5, -1, 2, 6], 4479 [ 5, 2, -1, -4], 4480 [-6, -2, 2, 6], 4481 [-5, -2, 2, 5], 4482 [ 6, 2, -1, -5], 4483 [-7, -2, 3, 8], 4484 [ 6, 2, -2, -6], 4485 [ 5, 2, -2, -5], 4486 [ 8, 3, -2, -7], 4487 [-9, -3, 3, 9] 4488 ]; 4489 4490 static immutable int8_t[5][8] silk_ltp_filter0_taps = [ 4491 [ 4, 6, 24, 7, 5], 4492 [ 0, 0, 2, 0, 0], 4493 [ 12, 28, 41, 13, -4], 4494 [ -9, 15, 42, 25, 14], 4495 [ 1, -2, 62, 41, -9], 4496 [-10, 37, 65, -4, 3], 4497 [ -6, 4, 66, 7, -8], 4498 [ 16, 14, 38, -3, 33] 4499 ]; 4500 4501 static immutable int8_t[5][16] silk_ltp_filter1_taps = [ 4502 [ 13, 22, 39, 23, 12], 4503 [ -1, 36, 64, 27, -6], 4504 [ -7, 10, 55, 43, 17], 4505 [ 1, 1, 8, 1, 1], 4506 [ 6, -11, 74, 53, -9], 4507 [-12, 55, 76, -12, 8], 4508 [ -3, 3, 93, 27, -4], 4509 [ 26, 39, 59, 3, -8], 4510 [ 2, 0, 77, 11, 9], 4511 [ -8, 22, 44, -6, 7], 4512 [ 40, 9, 26, 3, 9], 4513 [ -7, 20, 101, -7, 4], 4514 [ 3, -8, 42, 26, 0], 4515 [-15, 33, 68, 2, 23], 4516 [ -2, 55, 46, -2, 15], 4517 [ 3, -1, 21, 16, 41] 4518 ]; 4519 4520 static immutable int8_t[5][32] silk_ltp_filter2_taps = [ 4521 [ -6, 27, 61, 39, 5], 4522 [-11, 42, 88, 4, 1], 4523 [ -2, 60, 65, 6, -4], 4524 [ -1, -5, 73, 56, 1], 4525 [ -9, 19, 94, 29, -9], 4526 [ 0, 12, 99, 6, 4], 4527 [ 8, -19, 102, 46, -13], 4528 [ 3, 2, 13, 3, 2], 4529 [ 9, -21, 84, 72, -18], 4530 [-11, 46, 104, -22, 8], 4531 [ 18, 38, 48, 23, 0], 4532 [-16, 70, 83, -21, 11], 4533 [ 5, -11, 117, 22, -8], 4534 [ -6, 23, 117, -12, 3], 4535 [ 3, -8, 95, 28, 4], 4536 [-10, 15, 77, 60, -15], 4537 [ -1, 4, 124, 2, -4], 4538 [ 3, 38, 84, 24, -25], 4539 [ 2, 13, 42, 13, 31], 4540 [ 21, -4, 56, 46, -1], 4541 [ -1, 35, 79, -13, 19], 4542 [ -7, 65, 88, -9, -14], 4543 [ 20, 4, 81, 49, -29], 4544 [ 20, 0, 75, 3, -17], 4545 [ 5, -9, 44, 92, -8], 4546 [ 1, -3, 22, 69, 31], 4547 [ -6, 95, 41, -12, 5], 4548 [ 39, 67, 16, -4, 1], 4549 [ 0, -6, 120, 55, -36], 4550 [-13, 44, 122, 4, -24], 4551 [ 81, 5, 11, 3, 7], 4552 [ 2, 0, 9, 10, 88] 4553 ]; 4554 4555 static immutable uint16_t[3] silk_ltp_scale_factor = [15565, 12288, 8192]; 4556 4557 static immutable uint8_t[2][3] silk_shell_blocks = [ 4558 [ 5, 10], // NB 4559 [ 8, 15], // MB 4560 [10, 20] // WB 4561 ]; 4562 4563 static immutable uint8_t[2][2] silk_quant_offset = [ /* (0.23) */ 4564 [25, 60], // Inactive or Unvoiced 4565 [ 8, 25] // Voiced 4566 ]; 4567 4568 static immutable int[3] silk_stereo_interp_len = [ 4569 64, 96, 128 4570 ]; 4571 4572 4573 /*static inline*/ void silk_stabilize_lsf(int16_t* nlsf/*[16]*/, int order, const(uint16_t)* min_delta/*[17]*/) 4574 { 4575 int pass, i; 4576 for (pass = 0; pass < 20; pass++) { 4577 int k, min_diff = 0; 4578 for (i = 0; i < order+1; i++) { 4579 int low = i != 0 ? nlsf[i-1] : 0; 4580 int high = i != order ? nlsf[i] : 32768; 4581 int diff = (high - low) - (min_delta[i]); 4582 4583 if (diff < min_diff) { 4584 min_diff = diff; 4585 k = i; 4586 4587 if (pass == 20) 4588 break; 4589 } 4590 } 4591 if (min_diff == 0) /* no issues; stabilized */ 4592 return; 4593 4594 /* wiggle one or two LSFs */ 4595 if (k == 0) { 4596 /* repel away from lower bound */ 4597 nlsf[0] = min_delta[0]; 4598 } else if (k == order) { 4599 /* repel away from higher bound */ 4600 nlsf[order-1] = cast(short)(32768 - min_delta[order]); 4601 } else { 4602 /* repel away from current position */ 4603 int min_center = 0, max_center = 32768, center_val; 4604 4605 /* lower extent */ 4606 for (i = 0; i < k; i++) 4607 min_center += min_delta[i]; 4608 min_center += min_delta[k] >> 1; 4609 4610 /* upper extent */ 4611 for (i = order; i > k; i--) 4612 max_center -= min_delta[i]; 4613 max_center -= min_delta[k] >> 1; 4614 4615 /* move apart */ 4616 center_val = nlsf[k - 1] + nlsf[k]; 4617 center_val = (center_val >> 1) + (center_val & 1); // rounded divide by 2 4618 center_val = FFMIN(max_center, FFMAX(min_center, center_val)); 4619 4620 nlsf[k - 1] = cast(short)(center_val - (min_delta[k] >> 1)); 4621 nlsf[k] = cast(short)(nlsf[k - 1] + min_delta[k]); 4622 } 4623 } 4624 4625 /* resort to the fall-back method, the standard method for LSF stabilization */ 4626 4627 /* sort; as the LSFs should be nearly sorted, use insertion sort */ 4628 for (i = 1; i < order; i++) { 4629 int j, value = nlsf[i]; 4630 for (j = i - 1; j >= 0 && nlsf[j] > value; j--) 4631 nlsf[j + 1] = nlsf[j]; 4632 nlsf[j + 1] = cast(short)value; 4633 } 4634 4635 /* push forwards to increase distance */ 4636 if (nlsf[0] < min_delta[0]) 4637 nlsf[0] = min_delta[0]; 4638 for (i = 1; i < order; i++) 4639 if (nlsf[i] < nlsf[i - 1] + min_delta[i]) 4640 nlsf[i] = cast(short)(nlsf[i - 1] + min_delta[i]); 4641 4642 /* push backwards to increase distance */ 4643 if (nlsf[order-1] > 32768 - min_delta[order]) 4644 nlsf[order-1] = cast(short)(32768 - min_delta[order]); 4645 for (i = order-2; i >= 0; i--) 4646 if (nlsf[i] > nlsf[i + 1] - min_delta[i+1]) 4647 nlsf[i] = cast(short)(nlsf[i + 1] - min_delta[i+1]); 4648 4649 return; 4650 } 4651 4652 /*static inline*/ int silk_is_lpc_stable(const(int16_t)* lpc/*[16]*/, int order) 4653 { 4654 int k, j, DC_resp = 0; 4655 int32_t[16][2] lpc32; // Q24 4656 int totalinvgain = 1 << 30; // 1.0 in Q30 4657 int32_t *row = lpc32[0].ptr; 4658 int32_t *prevrow; 4659 4660 /* initialize the first row for the Levinson recursion */ 4661 for (k = 0; k < order; k++) { 4662 DC_resp += lpc[k]; 4663 row[k] = lpc[k] * 4096; 4664 } 4665 4666 if (DC_resp >= 4096) 4667 return 0; 4668 4669 /* check if prediction gain pushes any coefficients too far */ 4670 for (k = order - 1; 1; k--) { 4671 int rc; // Q31; reflection coefficient 4672 int gaindiv; // Q30; inverse of the gain (the divisor) 4673 int gain; // gain for this reflection coefficient 4674 int fbits; // fractional bits used for the gain 4675 int error; // Q29; estimate of the error of our partial estimate of 1/gaindiv 4676 4677 if (FFABS(row[k]) > 16773022) 4678 return 0; 4679 4680 rc = -(row[k] * 128); 4681 gaindiv = (1 << 30) - MULH(rc, rc); 4682 4683 totalinvgain = MULH(totalinvgain, gaindiv) << 2; 4684 if (k == 0) 4685 return (totalinvgain >= 107374); 4686 4687 /* approximate 1.0/gaindiv */ 4688 fbits = opus_ilog(gaindiv); 4689 gain = ((1 << 29) - 1) / (gaindiv >> (fbits + 1 - 16)); // Q<fbits-16> 4690 error = cast(int)((1 << 29) - MULL(gaindiv << (15 + 16 - fbits), gain, 16)); 4691 gain = ((gain << 16) + (error * gain >> 13)); 4692 4693 /* switch to the next row of the LPC coefficients */ 4694 prevrow = row; 4695 row = lpc32[k & 1].ptr; 4696 4697 for (j = 0; j < k; j++) { 4698 int x = cast(int)(prevrow[j] - ROUND_MULL(prevrow[k - j - 1], rc, 31)); 4699 row[j] = cast(int)(ROUND_MULL(x, gain, fbits)); 4700 } 4701 } 4702 } 4703 4704 static void silk_lsp2poly(const(int32_t)* lsp/*[16]*/, int32_t* pol/*[16]*/, int half_order) 4705 { 4706 int i, j; 4707 4708 pol[0] = 65536; // 1.0 in Q16 4709 pol[1] = -lsp[0]; 4710 4711 for (i = 1; i < half_order; i++) { 4712 pol[i + 1] = cast(int)(pol[i - 1] * 2 - ROUND_MULL(lsp[2 * i], pol[i], 16)); 4713 for (j = i; j > 1; j--) 4714 pol[j] += pol[j - 2] - ROUND_MULL(lsp[2 * i], pol[j - 1], 16); 4715 4716 pol[1] -= lsp[2 * i]; 4717 } 4718 } 4719 4720 static void silk_lsf2lpc(const(int16_t)* nlsf/*[16]*/, float* lpcf/*[16]*/, int order) 4721 { 4722 int i, k; 4723 int32_t[16] lsp; // Q17; 2*cos(LSF) 4724 int32_t[9] p, q; // Q16 4725 int32_t[16] lpc32; // Q17 4726 int16_t[16] lpc; // Q12 4727 4728 /* convert the LSFs to LSPs, i.e. 2*cos(LSF) */ 4729 for (k = 0; k < order; k++) { 4730 int index = nlsf[k] >> 8; 4731 int offset = nlsf[k] & 255; 4732 int k2 = (order == 10) ? silk_lsf_ordering_nbmb[k] : silk_lsf_ordering_wb[k]; 4733 4734 /* interpolate and round */ 4735 lsp[k2] = silk_cosine[index] * 256; 4736 lsp[k2] += (silk_cosine[index + 1] - silk_cosine[index]) * offset; 4737 lsp[k2] = (lsp[k2] + 4) >> 3; 4738 } 4739 4740 silk_lsp2poly(lsp.ptr , p.ptr, order >> 1); 4741 silk_lsp2poly(lsp.ptr + 1, q.ptr, order >> 1); 4742 4743 /* reconstruct A(z) */ 4744 for (k = 0; k < order>>1; k++) { 4745 lpc32[k] = -p[k + 1] - p[k] - q[k + 1] + q[k]; 4746 lpc32[order-k-1] = -p[k + 1] - p[k] + q[k + 1] - q[k]; 4747 } 4748 4749 /* limit the range of the LPC coefficients to each fit within an int16_t */ 4750 for (i = 0; i < 10; i++) { 4751 int j; 4752 uint maxabs = 0; 4753 for (j = 0, k = 0; j < order; j++) { 4754 uint x = FFABS(lpc32[k]); 4755 if (x > maxabs) { 4756 maxabs = x; // Q17 4757 k = j; 4758 } 4759 } 4760 4761 maxabs = (maxabs + 16) >> 5; // convert to Q12 4762 4763 if (maxabs > 32767) { 4764 /* perform bandwidth expansion */ 4765 uint chirp, chirp_base; // Q16 4766 maxabs = FFMIN(maxabs, 163838); // anything above this overflows chirp's numerator 4767 chirp_base = chirp = 65470 - ((maxabs - 32767) << 14) / ((maxabs * (k+1)) >> 2); 4768 4769 for (k = 0; k < order; k++) { 4770 lpc32[k] = cast(int)(ROUND_MULL(lpc32[k], chirp, 16)); 4771 chirp = (chirp_base * chirp + 32768) >> 16; 4772 } 4773 } else break; 4774 } 4775 4776 if (i == 10) { 4777 /* time's up: just clamp */ 4778 for (k = 0; k < order; k++) { 4779 int x = (lpc32[k] + 16) >> 5; 4780 lpc[k] = av_clip_int16(x); 4781 lpc32[k] = lpc[k] << 5; // shortcut mandated by the spec; drops lower 5 bits 4782 } 4783 } else { 4784 for (k = 0; k < order; k++) 4785 lpc[k] = cast(short)((lpc32[k] + 16) >> 5); 4786 } 4787 4788 /* if the prediction gain causes the LPC filter to become unstable, 4789 apply further bandwidth expansion on the Q17 coefficients */ 4790 for (i = 1; i <= 16 && !silk_is_lpc_stable(lpc.ptr, order); i++) { 4791 uint chirp, chirp_base; 4792 chirp_base = chirp = 65536 - (1 << i); 4793 4794 for (k = 0; k < order; k++) { 4795 lpc32[k] = cast(int)(ROUND_MULL(lpc32[k], chirp, 16)); 4796 lpc[k] = cast(short)((lpc32[k] + 16) >> 5); 4797 chirp = (chirp_base * chirp + 32768) >> 16; 4798 } 4799 } 4800 4801 for (i = 0; i < order; i++) 4802 lpcf[i] = lpc[i] / 4096.0f; 4803 } 4804 4805 /*static inline*/ void silk_decode_lpc(SilkContext *s, SilkFrame *frame, 4806 OpusRangeCoder *rc, 4807 float* lpc_leadin/*[16]*/, float* lpc/*[16]*/, 4808 int *lpc_order, int *has_lpc_leadin, int voiced) 4809 { 4810 int i; 4811 int order; // order of the LP polynomial; 10 for NB/MB and 16 for WB 4812 int8_t lsf_i1; 4813 int8_t[16] lsf_i2; // stage-1 and stage-2 codebook indices 4814 int16_t[16] lsf_res; // residual as a Q10 value 4815 int16_t[16] nlsf; // Q15 4816 4817 *lpc_order = order = s.wb ? 16 : 10; 4818 4819 /* obtain LSF stage-1 and stage-2 indices */ 4820 lsf_i1 = cast(byte)opus_rc_getsymbol(rc, silk_model_lsf_s1[s.wb][voiced].ptr); 4821 for (i = 0; i < order; i++) { 4822 int index = s.wb ? silk_lsf_s2_model_sel_wb [lsf_i1][i] : 4823 silk_lsf_s2_model_sel_nbmb[lsf_i1][i]; 4824 lsf_i2[i] = cast(byte)(opus_rc_getsymbol(rc, silk_model_lsf_s2[index].ptr) - 4); 4825 if (lsf_i2[i] == -4) 4826 lsf_i2[i] -= opus_rc_getsymbol(rc, silk_model_lsf_s2_ext.ptr); 4827 else if (lsf_i2[i] == 4) 4828 lsf_i2[i] += opus_rc_getsymbol(rc, silk_model_lsf_s2_ext.ptr); 4829 } 4830 4831 /* reverse the backwards-prediction step */ 4832 for (i = order - 1; i >= 0; i--) { 4833 int qstep = s.wb ? 9830 : 11796; 4834 4835 lsf_res[i] = cast(short)(lsf_i2[i] * 1024); 4836 if (lsf_i2[i] < 0) lsf_res[i] += 102; 4837 else if (lsf_i2[i] > 0) lsf_res[i] -= 102; 4838 lsf_res[i] = (lsf_res[i] * qstep) >> 16; 4839 4840 if (i + 1 < order) { 4841 int weight = s.wb ? silk_lsf_pred_weights_wb [silk_lsf_weight_sel_wb [lsf_i1][i]][i] : 4842 silk_lsf_pred_weights_nbmb[silk_lsf_weight_sel_nbmb[lsf_i1][i]][i]; 4843 lsf_res[i] += (lsf_res[i+1] * weight) >> 8; 4844 } 4845 } 4846 4847 /* reconstruct the NLSF coefficients from the supplied indices */ 4848 for (i = 0; i < order; i++) { 4849 const uint8_t * codebook = s.wb ? silk_lsf_codebook_wb[lsf_i1].ptr : silk_lsf_codebook_nbmb[lsf_i1].ptr; 4850 int cur, prev, next, weight_sq, weight, ipart, fpart, y, value; 4851 4852 /* find the weight of the residual */ 4853 /* TODO: precompute */ 4854 cur = codebook[i]; 4855 prev = i ? codebook[i - 1] : 0; 4856 next = i + 1 < order ? codebook[i + 1] : 256; 4857 weight_sq = (1024 / (cur - prev) + 1024 / (next - cur)) << 16; 4858 4859 /* approximate square-root with mandated fixed-point arithmetic */ 4860 ipart = opus_ilog(weight_sq); 4861 fpart = (weight_sq >> (ipart-8)) & 127; 4862 y = ((ipart & 1) ? 32768 : 46214) >> ((32 - ipart)>>1); 4863 weight = y + ((213 * fpart * y) >> 16); 4864 4865 value = cur * 128 + (lsf_res[i] * 16384) / weight; 4866 nlsf[i] = cast(short)av_clip_uintp2(value, 15); 4867 } 4868 4869 /* stabilize the NLSF coefficients */ 4870 silk_stabilize_lsf(nlsf.ptr, order, s.wb ? silk_lsf_min_spacing_wb.ptr : silk_lsf_min_spacing_nbmb.ptr); 4871 4872 /* produce an interpolation for the first 2 subframes, */ 4873 /* and then convert both sets of NLSFs to LPC coefficients */ 4874 *has_lpc_leadin = 0; 4875 if (s.subframes == 4) { 4876 int offset = opus_rc_getsymbol(rc, silk_model_lsf_interpolation_offset.ptr); 4877 if (offset != 4 && frame.coded) { 4878 *has_lpc_leadin = 1; 4879 if (offset != 0) { 4880 int16_t[16] nlsf_leadin; 4881 for (i = 0; i < order; i++) 4882 nlsf_leadin[i] = cast(short)(frame.nlsf[i] + ((nlsf[i] - frame.nlsf[i]) * offset >> 2)); 4883 silk_lsf2lpc(nlsf_leadin.ptr, lpc_leadin, order); 4884 } else /* avoid re-computation for a (roughly) 1-in-4 occurrence */ 4885 memcpy(lpc_leadin, frame.lpc.ptr, 16 * float.sizeof); 4886 } else 4887 offset = 4; 4888 s.nlsf_interp_factor = offset; 4889 4890 silk_lsf2lpc(nlsf.ptr, lpc, order); 4891 } else { 4892 s.nlsf_interp_factor = 4; 4893 silk_lsf2lpc(nlsf.ptr, lpc, order); 4894 } 4895 4896 memcpy(frame.nlsf.ptr, nlsf.ptr, order * nlsf[0].sizeof); 4897 memcpy(frame.lpc.ptr, lpc, order * lpc[0].sizeof); 4898 } 4899 4900 /*static inline*/ void silk_count_children(OpusRangeCoder *rc, int model, int32_t total, int32_t* child/*[2]*/) 4901 { 4902 if (total != 0) { 4903 child[0] = opus_rc_getsymbol(rc, silk_model_pulse_location[model].ptr + (((total - 1 + 5) * (total - 1)) >> 1)); 4904 child[1] = total - child[0]; 4905 } else { 4906 child[0] = 0; 4907 child[1] = 0; 4908 } 4909 } 4910 4911 /*static inline*/ void silk_decode_excitation(SilkContext *s, OpusRangeCoder *rc, 4912 float* excitationf, 4913 int qoffset_high, int active, int voiced) 4914 { 4915 int i; 4916 uint32_t seed; 4917 int shellblocks; 4918 int ratelevel; 4919 uint8_t[20] pulsecount; // total pulses in each shell block 4920 uint8_t[20] lsbcount = 0; // raw lsbits defined for each pulse in each shell block 4921 int32_t[320] excitation; // Q23 4922 4923 /* excitation parameters */ 4924 seed = opus_rc_getsymbol(rc, silk_model_lcg_seed.ptr); 4925 shellblocks = silk_shell_blocks[s.bandwidth][s.subframes >> 2]; 4926 ratelevel = opus_rc_getsymbol(rc, silk_model_exc_rate[voiced].ptr); 4927 4928 for (i = 0; i < shellblocks; i++) { 4929 pulsecount[i] = cast(ubyte)opus_rc_getsymbol(rc, silk_model_pulse_count[ratelevel].ptr); 4930 if (pulsecount[i] == 17) { 4931 while (pulsecount[i] == 17 && ++lsbcount[i] != 10) 4932 pulsecount[i] = cast(ubyte)opus_rc_getsymbol(rc, silk_model_pulse_count[9].ptr); 4933 if (lsbcount[i] == 10) 4934 pulsecount[i] = cast(ubyte)opus_rc_getsymbol(rc, silk_model_pulse_count[10].ptr); 4935 } 4936 } 4937 4938 /* decode pulse locations using PVQ */ 4939 for (i = 0; i < shellblocks; i++) { 4940 if (pulsecount[i] != 0) { 4941 int a, b, c, d; 4942 int32_t * location = excitation.ptr + 16*i; 4943 int32_t[2][4] branch; 4944 branch[0][0] = pulsecount[i]; 4945 4946 /* unrolled tail recursion */ 4947 for (a = 0; a < 1; a++) { 4948 silk_count_children(rc, 0, branch[0][a], branch[1].ptr); 4949 for (b = 0; b < 2; b++) { 4950 silk_count_children(rc, 1, branch[1][b], branch[2].ptr); 4951 for (c = 0; c < 2; c++) { 4952 silk_count_children(rc, 2, branch[2][c], branch[3].ptr); 4953 for (d = 0; d < 2; d++) { 4954 silk_count_children(rc, 3, branch[3][d], location); 4955 location += 2; 4956 } 4957 } 4958 } 4959 } 4960 } else 4961 memset(excitation.ptr + 16*i, 0, 16*int32_t.sizeof); 4962 } 4963 4964 /* decode least significant bits */ 4965 for (i = 0; i < shellblocks << 4; i++) { 4966 int bit; 4967 for (bit = 0; bit < lsbcount[i >> 4]; bit++) 4968 excitation[i] = (excitation[i] << 1) | 4969 opus_rc_getsymbol(rc, silk_model_excitation_lsb.ptr); 4970 } 4971 4972 /* decode signs */ 4973 for (i = 0; i < shellblocks << 4; i++) { 4974 if (excitation[i] != 0) { 4975 int sign = opus_rc_getsymbol(rc, silk_model_excitation_sign[active + voiced][qoffset_high][FFMIN(pulsecount[i >> 4], 6)].ptr); 4976 if (sign == 0) 4977 excitation[i] *= -1; 4978 } 4979 } 4980 4981 /* assemble the excitation */ 4982 for (i = 0; i < shellblocks << 4; i++) { 4983 int value = excitation[i]; 4984 excitation[i] = value * 256 | silk_quant_offset[voiced][qoffset_high]; 4985 if (value < 0) excitation[i] += 20; 4986 else if (value > 0) excitation[i] -= 20; 4987 4988 /* invert samples pseudorandomly */ 4989 seed = 196314165 * seed + 907633515; 4990 if (seed & 0x80000000) 4991 excitation[i] *= -1; 4992 seed += value; 4993 4994 excitationf[i] = excitation[i] / 8388608.0f; 4995 } 4996 } 4997 4998 /** Maximum residual history according to 4.2.7.6.1 */ 4999 enum SILK_MAX_LAG = (288 + LTP_ORDER / 2); 5000 5001 /** Order of the LTP filter */ 5002 enum LTP_ORDER = 5; 5003 5004 static void silk_decode_frame(SilkContext *s, OpusRangeCoder *rc, 5005 int frame_num, int channel, int coded_channels, int active, int active1) 5006 { 5007 /* per frame */ 5008 int voiced; // combines with active to indicate inactive, active, or active+voiced 5009 int qoffset_high; 5010 int order; // order of the LPC coefficients 5011 float[16] lpc_leadin; 5012 float[16] lpc_body; 5013 float[SILK_MAX_LAG + SILK_HISTORY] residual; 5014 int has_lpc_leadin; 5015 float ltpscale; 5016 5017 /* per subframe */ 5018 static struct SF { 5019 float gain; 5020 int pitchlag; 5021 float[5] ltptaps; 5022 } 5023 SF[4] sf = void; 5024 5025 //const(SilkFrame)* frame = s.frame.ptr + channel; 5026 SilkFrame* frame = s.frame.ptr + channel; 5027 5028 int i; 5029 5030 /* obtain stereo weights */ 5031 if (coded_channels == 2 && channel == 0) { 5032 int n; 5033 int[2] wi, ws, w; 5034 n = opus_rc_getsymbol(rc, silk_model_stereo_s1.ptr); 5035 wi[0] = opus_rc_getsymbol(rc, silk_model_stereo_s2.ptr) + 3 * (n / 5); 5036 ws[0] = opus_rc_getsymbol(rc, silk_model_stereo_s3.ptr); 5037 wi[1] = opus_rc_getsymbol(rc, silk_model_stereo_s2.ptr) + 3 * (n % 5); 5038 ws[1] = opus_rc_getsymbol(rc, silk_model_stereo_s3.ptr); 5039 5040 for (i = 0; i < 2; i++) 5041 w[i] = silk_stereo_weights[wi[i]] + 5042 (((silk_stereo_weights[wi[i] + 1] - silk_stereo_weights[wi[i]]) * 6554) >> 16) 5043 * (ws[i]*2 + 1); 5044 5045 s.stereo_weights[0] = (w[0] - w[1]) / 8192.0; 5046 s.stereo_weights[1] = w[1] / 8192.0; 5047 5048 /* and read the mid-only flag */ 5049 s.midonly = active1 ? 0 : opus_rc_getsymbol(rc, silk_model_mid_only.ptr); 5050 } 5051 5052 /* obtain frame type */ 5053 if (!active) { 5054 qoffset_high = opus_rc_getsymbol(rc, silk_model_frame_type_inactive.ptr); 5055 voiced = 0; 5056 } else { 5057 int type = opus_rc_getsymbol(rc, silk_model_frame_type_active.ptr); 5058 qoffset_high = type & 1; 5059 voiced = type >> 1; 5060 } 5061 5062 /* obtain subframe quantization gains */ 5063 for (i = 0; i < s.subframes; i++) { 5064 int log_gain; //Q7 5065 int ipart, fpart, lingain; 5066 5067 if (i == 0 && (frame_num == 0 || !frame.coded)) { 5068 /* gain is coded absolute */ 5069 int x = opus_rc_getsymbol(rc, silk_model_gain_highbits[active + voiced].ptr); 5070 log_gain = (x<<3) | opus_rc_getsymbol(rc, silk_model_gain_lowbits.ptr); 5071 5072 if (frame.coded) 5073 log_gain = FFMAX(log_gain, frame.log_gain - 16); 5074 } else { 5075 /* gain is coded relative */ 5076 int delta_gain = opus_rc_getsymbol(rc, silk_model_gain_delta.ptr); 5077 log_gain = av_clip_uintp2(FFMAX((delta_gain<<1) - 16, 5078 frame.log_gain + delta_gain - 4), 6); 5079 } 5080 5081 frame.log_gain = log_gain; 5082 5083 /* approximate 2**(x/128) with a Q7 (i.e. non-integer) input */ 5084 log_gain = (log_gain * 0x1D1C71 >> 16) + 2090; 5085 ipart = log_gain >> 7; 5086 fpart = log_gain & 127; 5087 lingain = (1 << ipart) + ((-174 * fpart * (128-fpart) >>16) + fpart) * ((1<<ipart) >> 7); 5088 sf[i].gain = lingain / 65536.0f; 5089 } 5090 5091 /* obtain LPC filter coefficients */ 5092 silk_decode_lpc(s, frame, rc, lpc_leadin.ptr, lpc_body.ptr, &order, &has_lpc_leadin, voiced); 5093 5094 /* obtain pitch lags, if this is a voiced frame */ 5095 if (voiced) { 5096 int lag_absolute = (!frame_num || !frame.prev_voiced); 5097 int primarylag; // primary pitch lag for the entire SILK frame 5098 int ltpfilter; 5099 const(int8_t)* offsets; 5100 5101 if (!lag_absolute) { 5102 int delta = opus_rc_getsymbol(rc, silk_model_pitch_delta.ptr); 5103 if (delta) 5104 primarylag = frame.primarylag + delta - 9; 5105 else 5106 lag_absolute = 1; 5107 } 5108 5109 if (lag_absolute) { 5110 /* primary lag is coded absolute */ 5111 int highbits, lowbits; 5112 static immutable uint16_t*[3] model = [ 5113 silk_model_pitch_lowbits_nb.ptr, silk_model_pitch_lowbits_mb.ptr, 5114 silk_model_pitch_lowbits_wb.ptr 5115 ]; 5116 highbits = opus_rc_getsymbol(rc, silk_model_pitch_highbits.ptr); 5117 lowbits = opus_rc_getsymbol(rc, model[s.bandwidth]); 5118 5119 primarylag = silk_pitch_min_lag[s.bandwidth] + 5120 highbits*silk_pitch_scale[s.bandwidth] + lowbits; 5121 } 5122 frame.primarylag = primarylag; 5123 5124 if (s.subframes == 2) 5125 offsets = (s.bandwidth == OPUS_BANDWIDTH_NARROWBAND) 5126 ? silk_pitch_offset_nb10ms[opus_rc_getsymbol(rc, silk_model_pitch_contour_nb10ms.ptr)].ptr 5127 : silk_pitch_offset_mbwb10ms[opus_rc_getsymbol(rc, silk_model_pitch_contour_mbwb10ms.ptr)].ptr; 5128 else 5129 offsets = (s.bandwidth == OPUS_BANDWIDTH_NARROWBAND) 5130 ? silk_pitch_offset_nb20ms[opus_rc_getsymbol(rc, silk_model_pitch_contour_nb20ms.ptr)].ptr 5131 : silk_pitch_offset_mbwb20ms[opus_rc_getsymbol(rc, silk_model_pitch_contour_mbwb20ms.ptr)].ptr; 5132 5133 for (i = 0; i < s.subframes; i++) 5134 sf[i].pitchlag = av_clip(primarylag + offsets[i], 5135 silk_pitch_min_lag[s.bandwidth], 5136 silk_pitch_max_lag[s.bandwidth]); 5137 5138 /* obtain LTP filter coefficients */ 5139 ltpfilter = opus_rc_getsymbol(rc, silk_model_ltp_filter.ptr); 5140 for (i = 0; i < s.subframes; i++) { 5141 int index, j; 5142 static immutable uint16_t*[3] filter_sel = [ 5143 silk_model_ltp_filter0_sel.ptr, silk_model_ltp_filter1_sel.ptr, 5144 silk_model_ltp_filter2_sel.ptr 5145 ]; 5146 static immutable int8_t[5]*[3] /*(*filter_taps[])[5]*/ filter_taps = [ 5147 silk_ltp_filter0_taps.ptr, silk_ltp_filter1_taps.ptr, silk_ltp_filter2_taps.ptr 5148 ]; 5149 index = opus_rc_getsymbol(rc, filter_sel[ltpfilter]); 5150 for (j = 0; j < 5; j++) 5151 sf[i].ltptaps[j] = filter_taps[ltpfilter][index][j] / 128.0f; 5152 } 5153 } 5154 5155 /* obtain LTP scale factor */ 5156 if (voiced && frame_num == 0) 5157 ltpscale = silk_ltp_scale_factor[opus_rc_getsymbol(rc, silk_model_ltp_scale_index.ptr)] / 16384.0f; 5158 else ltpscale = 15565.0f/16384.0f; 5159 5160 /* generate the excitation signal for the entire frame */ 5161 silk_decode_excitation(s, rc, residual.ptr + SILK_MAX_LAG, qoffset_high, active, voiced); 5162 5163 /* skip synthesising the side channel if we want mono-only */ 5164 if (s.output_channels == channel) 5165 return; 5166 5167 /* generate the output signal */ 5168 for (i = 0; i < s.subframes; i++) { 5169 const(float)* lpc_coeff = (i < 2 && has_lpc_leadin) ? lpc_leadin.ptr : lpc_body.ptr; 5170 float *dst = frame.output.ptr + SILK_HISTORY + i * s.sflength; 5171 float *resptr = residual.ptr + SILK_MAX_LAG + i * s.sflength; 5172 float *lpc = frame.lpc_history.ptr + SILK_HISTORY + i * s.sflength; 5173 float sum; 5174 int j, k; 5175 5176 if (voiced) { 5177 int out_end; 5178 float scale; 5179 5180 if (i < 2 || s.nlsf_interp_factor == 4) { 5181 out_end = -i * s.sflength; 5182 scale = ltpscale; 5183 } else { 5184 out_end = -(i - 2) * s.sflength; 5185 scale = 1.0f; 5186 } 5187 5188 /* when the LPC coefficients change, a re-whitening filter is used */ 5189 /* to produce a residual that accounts for the change */ 5190 for (j = - sf[i].pitchlag - LTP_ORDER/2; j < out_end; j++) { 5191 sum = dst[j]; 5192 for (k = 0; k < order; k++) 5193 sum -= lpc_coeff[k] * dst[j - k - 1]; 5194 resptr[j] = av_clipf(sum, -1.0f, 1.0f) * scale / sf[i].gain; 5195 } 5196 5197 if (out_end) { 5198 float rescale = sf[i-1].gain / sf[i].gain; 5199 for (j = out_end; j < 0; j++) 5200 resptr[j] *= rescale; 5201 } 5202 5203 /* LTP synthesis */ 5204 for (j = 0; j < s.sflength; j++) { 5205 sum = resptr[j]; 5206 for (k = 0; k < LTP_ORDER; k++) 5207 sum += sf[i].ltptaps[k] * resptr[j - sf[i].pitchlag + LTP_ORDER/2 - k]; 5208 resptr[j] = sum; 5209 } 5210 } 5211 5212 /* LPC synthesis */ 5213 for (j = 0; j < s.sflength; j++) { 5214 sum = resptr[j] * sf[i].gain; 5215 for (k = 1; k <= order; k++) 5216 sum += lpc_coeff[k - 1] * lpc[j - k]; 5217 5218 lpc[j] = sum; 5219 dst[j] = av_clipf(sum, -1.0f, 1.0f); 5220 } 5221 } 5222 5223 frame.prev_voiced = voiced; 5224 memmove(frame.lpc_history.ptr, frame.lpc_history.ptr + s.flength, SILK_HISTORY * float.sizeof); 5225 memmove(frame.output.ptr, frame.output.ptr + s.flength, SILK_HISTORY * float.sizeof); 5226 5227 frame.coded = 1; 5228 } 5229 5230 static void silk_unmix_ms(SilkContext *s, float *l, float *r) 5231 { 5232 float *mid = s.frame[0].output.ptr + SILK_HISTORY - s.flength; 5233 float *side = s.frame[1].output.ptr + SILK_HISTORY - s.flength; 5234 float w0_prev = s.prev_stereo_weights[0]; 5235 float w1_prev = s.prev_stereo_weights[1]; 5236 float w0 = s.stereo_weights[0]; 5237 float w1 = s.stereo_weights[1]; 5238 int n1 = silk_stereo_interp_len[s.bandwidth]; 5239 int i; 5240 5241 for (i = 0; i < n1; i++) { 5242 float interp0 = w0_prev + i * (w0 - w0_prev) / n1; 5243 float interp1 = w1_prev + i * (w1 - w1_prev) / n1; 5244 float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]); 5245 5246 l[i] = av_clipf((1 + interp1) * mid[i - 1] + side[i - 1] + interp0 * p0, -1.0, 1.0); 5247 r[i] = av_clipf((1 - interp1) * mid[i - 1] - side[i - 1] - interp0 * p0, -1.0, 1.0); 5248 } 5249 5250 for (; i < s.flength; i++) { 5251 float p0 = 0.25 * (mid[i - 2] + 2 * mid[i - 1] + mid[i]); 5252 5253 l[i] = av_clipf((1 + w1) * mid[i - 1] + side[i - 1] + w0 * p0, -1.0, 1.0); 5254 r[i] = av_clipf((1 - w1) * mid[i - 1] - side[i - 1] - w0 * p0, -1.0, 1.0); 5255 } 5256 5257 memcpy(s.prev_stereo_weights.ptr, s.stereo_weights.ptr, s.stereo_weights.sizeof); 5258 } 5259 5260 static void silk_flush_frame(SilkFrame *frame) 5261 { 5262 if (!frame.coded) 5263 return; 5264 5265 memset(frame.output.ptr, 0, frame.output.sizeof); 5266 memset(frame.lpc_history.ptr, 0, frame.lpc_history.sizeof); 5267 5268 memset(frame.lpc.ptr, 0, frame.lpc.sizeof); 5269 memset(frame.nlsf.ptr, 0, frame.nlsf.sizeof); 5270 5271 frame.log_gain = 0; 5272 5273 frame.primarylag = 0; 5274 frame.prev_voiced = 0; 5275 frame.coded = 0; 5276 } 5277 5278 int ff_silk_decode_superframe(SilkContext *s, OpusRangeCoder *rc, 5279 float** output/*[2]*/, 5280 OpusBandwidth bandwidth, 5281 int coded_channels, 5282 int duration_ms) 5283 { 5284 int[6][2] active; 5285 int[2] redundancy; 5286 int nb_frames, i, j; 5287 5288 if (bandwidth > OPUS_BANDWIDTH_WIDEBAND || 5289 coded_channels > 2 || duration_ms > 60) { 5290 //av_log(s.avctx, AV_LOG_ERROR, "Invalid parameters passed to the SILK decoder.\n"); 5291 return AVERROR(EINVAL); 5292 } 5293 5294 nb_frames = 1 + (duration_ms > 20) + (duration_ms > 40); 5295 s.subframes = duration_ms / nb_frames / 5; // 5ms subframes 5296 s.sflength = 20 * (bandwidth + 2); 5297 s.flength = s.sflength * s.subframes; 5298 s.bandwidth = bandwidth; 5299 s.wb = bandwidth == OPUS_BANDWIDTH_WIDEBAND; 5300 5301 /* make sure to flush the side channel when switching from mono to stereo */ 5302 if (coded_channels > s.prev_coded_channels) 5303 silk_flush_frame(&s.frame[1]); 5304 s.prev_coded_channels = coded_channels; 5305 5306 /* read the LP-layer header bits */ 5307 for (i = 0; i < coded_channels; i++) { 5308 for (j = 0; j < nb_frames; j++) 5309 active[i][j] = opus_rc_p2model(rc, 1); 5310 5311 redundancy[i] = opus_rc_p2model(rc, 1); 5312 if (redundancy[i]) { 5313 //av_log(s.avctx, AV_LOG_ERROR, "LBRR frames present; this is unsupported\n"); 5314 return AVERROR_PATCHWELCOME; 5315 } 5316 } 5317 5318 for (i = 0; i < nb_frames; i++) { 5319 for (j = 0; j < coded_channels && !s.midonly; j++) 5320 silk_decode_frame(s, rc, i, j, coded_channels, active[j][i], active[1][i]); 5321 5322 /* reset the side channel if it is not coded */ 5323 if (s.midonly && s.frame[1].coded) 5324 silk_flush_frame(&s.frame[1]); 5325 5326 if (coded_channels == 1 || s.output_channels == 1) { 5327 for (j = 0; j < s.output_channels; j++) { 5328 memcpy(output[j] + i * s.flength, s.frame[0].output.ptr + SILK_HISTORY - s.flength - 2, s.flength * float.sizeof); 5329 } 5330 } else { 5331 silk_unmix_ms(s, output[0] + i * s.flength, output[1] + i * s.flength); 5332 } 5333 5334 s.midonly = 0; 5335 } 5336 5337 return nb_frames * s.flength; 5338 } 5339 5340 void ff_silk_free(SilkContext **ps) 5341 { 5342 av_freep(ps); 5343 } 5344 5345 void ff_silk_flush(SilkContext *s) 5346 { 5347 silk_flush_frame(&s.frame[0]); 5348 silk_flush_frame(&s.frame[1]); 5349 5350 memset(s.prev_stereo_weights.ptr, 0, s.prev_stereo_weights.sizeof); 5351 } 5352 5353 int ff_silk_init(/*AVCodecContext *avctx,*/ SilkContext **ps, int output_channels) 5354 { 5355 SilkContext *s; 5356 5357 if (output_channels != 1 && output_channels != 2) { 5358 //av_log(avctx, AV_LOG_ERROR, "Invalid number of output channels: %d\n", output_channels); 5359 return AVERROR(EINVAL); 5360 } 5361 5362 s = av_mallocz!SilkContext(); 5363 if (!s) 5364 return AVERROR(ENOMEM); 5365 5366 //s.avctx = avctx; 5367 s.output_channels = output_channels; 5368 5369 ff_silk_flush(s); 5370 5371 *ps = s; 5372 5373 return 0; 5374 } 5375 5376 5377 version = sincresample_use_full_table; 5378 5379 5380 // ////////////////////////////////////////////////////////////////////////// // 5381 public struct OpusResampler { 5382 nothrow @nogc: 5383 public: 5384 alias Quality = int; 5385 enum : uint { 5386 Fastest = 0, 5387 Voip = 3, 5388 Default = 4, 5389 Desktop = 5, 5390 Best = 10, 5391 } 5392 5393 enum Error { 5394 OK = 0, 5395 NoMemory, 5396 BadState, 5397 BadArgument, 5398 BadData, 5399 } 5400 5401 private: 5402 nothrow @trusted @nogc: 5403 alias ResamplerFn = int function (ref OpusResampler st, uint chanIdx, const(float)* indata, uint *indataLen, float *outdata, uint *outdataLen); 5404 5405 private: 5406 uint inRate; 5407 uint outRate; 5408 uint numRate; // from 5409 uint denRate; // to 5410 5411 Quality srQuality; 5412 uint chanCount; 5413 uint filterLen; 5414 uint memAllocSize; 5415 uint bufferSize; 5416 int intAdvance; 5417 int fracAdvance; 5418 float cutoff; 5419 uint oversample; 5420 bool started; 5421 5422 // these are per-channel 5423 int[64] lastSample; 5424 uint[64] sampFracNum; 5425 uint[64] magicSamples; 5426 5427 float* mem; 5428 uint realMemLen; // how much memory really allocated 5429 float* sincTable; 5430 uint sincTableLen; 5431 uint realSincTableLen; // how much memory really allocated 5432 ResamplerFn resampler; 5433 5434 int inStride; 5435 int outStride; 5436 5437 public: 5438 static string errorStr (int err) { 5439 switch (err) with (Error) { 5440 case OK: return "success"; 5441 case NoMemory: return "memory allocation failed"; 5442 case BadState: return "bad resampler state"; 5443 case BadArgument: return "invalid argument"; 5444 case BadData: return "bad data passed"; 5445 default: 5446 } 5447 return "unknown error"; 5448 } 5449 5450 public: 5451 @disable this (this); 5452 ~this () { deinit(); } 5453 5454 bool inited () const pure { return (resampler !is null); } 5455 5456 void deinit () { 5457 if (mem !is null) { free(mem); mem = null; } 5458 if (sincTable !is null) { free(sincTable); sincTable = null; } 5459 /* 5460 memAllocSize = realMemLen = 0; 5461 sincTableLen = realSincTableLen = 0; 5462 resampler = null; 5463 started = false; 5464 */ 5465 inRate = outRate = numRate = denRate = 0; 5466 srQuality = cast(Quality)666; 5467 chanCount = 0; 5468 filterLen = 0; 5469 memAllocSize = 0; 5470 bufferSize = 0; 5471 intAdvance = 0; 5472 fracAdvance = 0; 5473 cutoff = 0; 5474 oversample = 0; 5475 started = 0; 5476 5477 mem = null; 5478 realMemLen = 0; // how much memory really allocated 5479 sincTable = null; 5480 sincTableLen = 0; 5481 realSincTableLen = 0; // how much memory really allocated 5482 resampler = null; 5483 5484 inStride = outStride = 0; 5485 } 5486 5487 /** Create a new resampler with integer input and output rates. 5488 * 5489 * Params: 5490 * chans = Number of channels to be processed 5491 * inRate = Input sampling rate (integer number of Hz). 5492 * outRate = Output sampling rate (integer number of Hz). 5493 * aquality = Resampling quality between 0 and 10, where 0 has poor quality and 10 has very high quality. 5494 * 5495 * Returns: 5496 * 0 or error code 5497 */ 5498 Error setup (uint chans, uint ainRate, uint aoutRate, Quality aquality/*, size_t line=__LINE__*/) { 5499 5500 deinit(); 5501 if (aquality < 0) aquality = 0; 5502 if (aquality > OpusResampler.Best) aquality = OpusResampler.Best; 5503 if (chans < 1 || chans > 16) return Error.BadArgument; 5504 5505 started = false; 5506 inRate = 0; 5507 outRate = 0; 5508 numRate = 0; 5509 denRate = 0; 5510 srQuality = cast(Quality)666; // it's ok 5511 sincTableLen = 0; 5512 memAllocSize = 0; 5513 filterLen = 0; 5514 mem = null; 5515 resampler = null; 5516 5517 cutoff = 1.0f; 5518 chanCount = chans; 5519 inStride = 1; 5520 outStride = 1; 5521 5522 bufferSize = 160; 5523 5524 // per channel data 5525 lastSample[] = 0; 5526 magicSamples[] = 0; 5527 sampFracNum[] = 0; 5528 5529 setQuality(aquality); 5530 setRate(ainRate, aoutRate); 5531 5532 if (auto filterErr = updateFilter()) { deinit(); return filterErr; } 5533 skipZeros(); // make sure that the first samples to go out of the resamplers don't have leading zeros 5534 5535 return Error.OK; 5536 } 5537 5538 /** Set (change) the input/output sampling rates (integer value). 5539 * 5540 * Params: 5541 * ainRate = Input sampling rate (integer number of Hz). 5542 * aoutRate = Output sampling rate (integer number of Hz). 5543 * 5544 * Returns: 5545 * 0 or error code 5546 */ 5547 Error setRate (uint ainRate, uint aoutRate/*, size_t line=__LINE__*/) { 5548 if (inRate == ainRate && outRate == aoutRate) return Error.OK; 5549 5550 uint oldDen = denRate; 5551 inRate = ainRate; 5552 outRate = aoutRate; 5553 auto div = gcd(ainRate, aoutRate); 5554 numRate = ainRate/div; 5555 denRate = aoutRate/div; 5556 5557 if (oldDen > 0) { 5558 foreach (ref v; sampFracNum.ptr[0..chanCount]) { 5559 v = v*denRate/oldDen; 5560 // safety net 5561 if (v >= denRate) v = denRate-1; 5562 } 5563 } 5564 5565 return (inited ? updateFilter() : Error.OK); 5566 } 5567 5568 /** Get the current input/output sampling rates (integer value). 5569 * 5570 * Params: 5571 * ainRate = Input sampling rate (integer number of Hz) copied. 5572 * aoutRate = Output sampling rate (integer number of Hz) copied. 5573 */ 5574 void getRate (out uint ainRate, out uint aoutRate) { 5575 ainRate = inRate; 5576 aoutRate = outRate; 5577 } 5578 5579 uint getInRate () { return inRate; } 5580 uint getOutRate () { return outRate; } 5581 5582 uint getChans () { return chanCount; } 5583 5584 /** Get the current resampling ratio. This will be reduced to the least common denominator. 5585 * 5586 * Params: 5587 * ratioNum = Numerator of the sampling rate ratio copied 5588 * ratioDen = Denominator of the sampling rate ratio copied 5589 */ 5590 void getRatio (out uint ratioNum, out uint ratioDen) { 5591 ratioNum = numRate; 5592 ratioDen = denRate; 5593 } 5594 5595 /** Set (change) the conversion quality. 5596 * 5597 * Params: 5598 * quality = Resampling quality between 0 and 10, where 0 has poor quality and 10 has very high quality. 5599 * 5600 * Returns: 5601 * 0 or error code 5602 */ 5603 Error setQuality (Quality aquality) { 5604 if (aquality < 0) aquality = 0; 5605 if (aquality > OpusResampler.Best) aquality = OpusResampler.Best; 5606 if (srQuality == aquality) return Error.OK; 5607 srQuality = aquality; 5608 return (inited ? updateFilter() : Error.OK); 5609 } 5610 5611 /** Get the conversion quality. 5612 * 5613 * Returns: 5614 * Resampling quality between 0 and 10, where 0 has poor quality and 10 has very high quality. 5615 */ 5616 int getQuality () { return srQuality; } 5617 5618 /** Get the latency introduced by the resampler measured in input samples. 5619 * 5620 * Returns: 5621 * Input latency; 5622 */ 5623 int inputLatency () { return filterLen/2; } 5624 5625 /** Get the latency introduced by the resampler measured in output samples. 5626 * 5627 * Returns: 5628 * Output latency. 5629 */ 5630 int outputLatency () { return ((filterLen/2)*denRate+(numRate>>1))/numRate; } 5631 5632 /* Make sure that the first samples to go out of the resamplers don't have 5633 * leading zeros. This is only useful before starting to use a newly created 5634 * resampler. It is recommended to use that when resampling an audio file, as 5635 * it will generate a file with the same length. For real-time processing, 5636 * it is probably easier not to use this call (so that the output duration 5637 * is the same for the first frame). 5638 * 5639 * Setup/reset sequence will automatically call this, so it is private. 5640 */ 5641 private void skipZeros () { foreach (immutable i; 0..chanCount) lastSample.ptr[i] = filterLen/2; } 5642 5643 static struct Data { 5644 const(float)[] dataIn; 5645 float[] dataOut; 5646 uint inputSamplesUsed; // out value, in samples (i.e. multiplied by channel count) 5647 uint outputSamplesUsed; // out value, in samples (i.e. multiplied by channel count) 5648 } 5649 5650 /** Resample (an interleaved) float array. The input and output buffers must *not* overlap. 5651 * `data.dataIn` can be empty, but `data.dataOut` can't. 5652 * Function will return number of consumed samples (*not* *frames*!) in `data.inputSamplesUsed`, 5653 * and number of produced samples in `data.outputSamplesUsed`. 5654 * You should provide enough samples for all channels, and all channels will be processed. 5655 * 5656 * Params: 5657 * data = input and output buffers, number of frames consumed and produced 5658 * 5659 * Returns: 5660 * 0 or error code 5661 */ 5662 Error process(string mode="interleaved") (ref Data data) { 5663 static assert(mode == "interleaved" || mode == "sequential"); 5664 5665 data.inputSamplesUsed = data.outputSamplesUsed = 0; 5666 if (!inited) return Error.BadState; 5667 5668 if (data.dataIn.length%chanCount || data.dataOut.length < 1 || data.dataOut.length%chanCount) return Error.BadData; 5669 if (data.dataIn.length > uint.max/4 || data.dataOut.length > uint.max/4) return Error.BadData; 5670 5671 static if (mode == "interleaved") { 5672 inStride = outStride = chanCount; 5673 } else { 5674 inStride = outStride = 1; 5675 } 5676 uint iofs = 0, oofs = 0; 5677 immutable uint idclen = cast(uint)(data.dataIn.length/chanCount); 5678 immutable uint odclen = cast(uint)(data.dataOut.length/chanCount); 5679 foreach (immutable i; 0..chanCount) { 5680 data.inputSamplesUsed = idclen; 5681 data.outputSamplesUsed = odclen; 5682 if (data.dataIn.length) { 5683 processOneChannel(i, data.dataIn.ptr+iofs, &data.inputSamplesUsed, data.dataOut.ptr+oofs, &data.outputSamplesUsed); 5684 } else { 5685 processOneChannel(i, null, &data.inputSamplesUsed, data.dataOut.ptr+oofs, &data.outputSamplesUsed); 5686 } 5687 static if (mode == "interleaved") { 5688 ++iofs; 5689 ++oofs; 5690 } else { 5691 iofs += idclen; 5692 oofs += odclen; 5693 } 5694 } 5695 data.inputSamplesUsed *= chanCount; 5696 data.outputSamplesUsed *= chanCount; 5697 return Error.OK; 5698 } 5699 5700 5701 //HACK for libswresample 5702 // return -1 or number of outframes 5703 int swrconvert (float** outbuf, int outframes, const(float)**inbuf, int inframes) { 5704 if (!inited || outframes < 1 || inframes < 0) return -1; 5705 inStride = outStride = 1; 5706 Data data; 5707 foreach (immutable i; 0..chanCount) { 5708 data.dataIn = (inframes ? inbuf[i][0..inframes] : null); 5709 data.dataOut = (outframes ? outbuf[i][0..outframes] : null); 5710 data.inputSamplesUsed = inframes; 5711 data.outputSamplesUsed = outframes; 5712 if (inframes > 0) { 5713 processOneChannel(i, data.dataIn.ptr, &data.inputSamplesUsed, data.dataOut.ptr, &data.outputSamplesUsed); 5714 } else { 5715 processOneChannel(i, null, &data.inputSamplesUsed, data.dataOut.ptr, &data.outputSamplesUsed); 5716 } 5717 } 5718 return data.outputSamplesUsed; 5719 } 5720 5721 /// Reset a resampler so a new (unrelated) stream can be processed. 5722 void reset () { 5723 lastSample[] = 0; 5724 magicSamples[] = 0; 5725 sampFracNum[] = 0; 5726 //foreach (immutable i; 0..chanCount*(filterLen-1)) mem[i] = 0; 5727 if (mem !is null) mem[0..chanCount*(filterLen-1)] = 0; 5728 skipZeros(); // make sure that the first samples to go out of the resamplers don't have leading zeros 5729 } 5730 5731 private: 5732 Error processOneChannel (uint chanIdx, const(float)* indata, uint* indataLen, float* outdata, uint* outdataLen) { 5733 uint ilen = *indataLen; 5734 uint olen = *outdataLen; 5735 float* x = mem+chanIdx*memAllocSize; 5736 const int filterOfs = filterLen-1; 5737 const uint xlen = memAllocSize-filterOfs; 5738 const int istride = inStride; 5739 if (magicSamples.ptr[chanIdx]) olen -= magic(chanIdx, &outdata, olen); 5740 if (!magicSamples.ptr[chanIdx]) { 5741 while (ilen && olen) { 5742 uint ichunk = (ilen > xlen ? xlen : ilen); 5743 uint ochunk = olen; 5744 if (indata !is null) { 5745 foreach (immutable j; 0..ichunk) x[j+filterOfs] = indata[j*istride]; 5746 } else { 5747 foreach (immutable j; 0..ichunk) x[j+filterOfs] = 0; 5748 } 5749 processNative(chanIdx, &ichunk, outdata, &ochunk); 5750 ilen -= ichunk; 5751 olen -= ochunk; 5752 outdata += ochunk*outStride; 5753 if (indata) indata += ichunk*istride; 5754 } 5755 } 5756 *indataLen -= ilen; 5757 *outdataLen -= olen; 5758 return Error.OK; 5759 } 5760 5761 Error processNative (uint chanIdx, uint* indataLen, float* outdata, uint* outdataLen) { 5762 immutable N = filterLen; 5763 int outSample = 0; 5764 float* x = mem+chanIdx*memAllocSize; 5765 uint ilen; 5766 5767 started = true; 5768 5769 // call the right resampler through the function ptr 5770 outSample = resampler(this, chanIdx, x, indataLen, outdata, outdataLen); 5771 5772 if (lastSample.ptr[chanIdx] < cast(int)*indataLen) *indataLen = lastSample.ptr[chanIdx]; 5773 *outdataLen = outSample; 5774 lastSample.ptr[chanIdx] -= *indataLen; 5775 5776 ilen = *indataLen; 5777 5778 foreach (immutable j; 0..N-1) x[j] = x[j+ilen]; 5779 5780 return Error.OK; 5781 } 5782 5783 int magic (uint chanIdx, float **outdata, uint outdataLen) { 5784 uint tempInLen = magicSamples.ptr[chanIdx]; 5785 float* x = mem+chanIdx*memAllocSize; 5786 processNative(chanIdx, &tempInLen, *outdata, &outdataLen); 5787 magicSamples.ptr[chanIdx] -= tempInLen; 5788 // if we couldn't process all "magic" input samples, save the rest for next time 5789 if (magicSamples.ptr[chanIdx]) { 5790 immutable N = filterLen; 5791 foreach (immutable i; 0..magicSamples.ptr[chanIdx]) x[N-1+i] = x[N-1+i+tempInLen]; 5792 } 5793 *outdata += outdataLen*outStride; 5794 return outdataLen; 5795 } 5796 5797 Error updateFilter () { 5798 uint oldFilterLen = filterLen; 5799 uint oldAllocSize = memAllocSize; 5800 bool useDirect; 5801 uint minSincTableLen; 5802 uint minAllocSize; 5803 5804 intAdvance = numRate/denRate; 5805 fracAdvance = numRate%denRate; 5806 oversample = qualityMap.ptr[srQuality].oversample; 5807 filterLen = qualityMap.ptr[srQuality].baseLength; 5808 5809 if (numRate > denRate) { 5810 // down-sampling 5811 cutoff = qualityMap.ptr[srQuality].downsampleBandwidth*denRate/numRate; 5812 // FIXME: divide the numerator and denominator by a certain amount if they're too large 5813 filterLen = filterLen*numRate/denRate; 5814 // Round up to make sure we have a multiple of 8 for SSE 5815 filterLen = ((filterLen-1)&(~0x7))+8; 5816 if (2*denRate < numRate) oversample >>= 1; 5817 if (4*denRate < numRate) oversample >>= 1; 5818 if (8*denRate < numRate) oversample >>= 1; 5819 if (16*denRate < numRate) oversample >>= 1; 5820 if (oversample < 1) oversample = 1; 5821 } else { 5822 // up-sampling 5823 cutoff = qualityMap.ptr[srQuality].upsampleBandwidth; 5824 } 5825 5826 // choose the resampling type that requires the least amount of memory 5827 version(sincresample_use_full_table) { 5828 useDirect = true; 5829 if (int.max/float.sizeof/denRate < filterLen) goto fail; 5830 } else { 5831 useDirect = (filterLen*denRate <= filterLen*oversample+8 && int.max/float.sizeof/denRate >= filterLen); 5832 } 5833 5834 if (useDirect) { 5835 minSincTableLen = filterLen*denRate; 5836 } else { 5837 if ((int.max/float.sizeof-8)/oversample < filterLen) goto fail; 5838 minSincTableLen = filterLen*oversample+8; 5839 } 5840 5841 if (sincTableLen < minSincTableLen) { 5842 auto nslen = cast(uint)(minSincTableLen*float.sizeof); 5843 if (nslen > realSincTableLen) { 5844 if (nslen < 512*1024) nslen = 512*1024; // inc to 3 mb? 5845 auto x = cast(float*)realloc(sincTable, nslen); 5846 if (!x) goto fail; 5847 sincTable = x; 5848 realSincTableLen = nslen; 5849 } 5850 sincTableLen = minSincTableLen; 5851 } 5852 5853 if (useDirect) { 5854 foreach (int i; 0..denRate) { 5855 foreach (int j; 0..filterLen) { 5856 sincTable[i*filterLen+j] = sinc(cutoff, ((j-cast(int)filterLen/2+1)-(cast(float)i)/denRate), filterLen, qualityMap.ptr[srQuality].windowFunc); 5857 } 5858 } 5859 if (srQuality > 8) { 5860 resampler = &resamplerBasicDirect!double; 5861 } else { 5862 resampler = &resamplerBasicDirect!float; 5863 } 5864 } else { 5865 foreach (immutable int i; -4..cast(int)(oversample*filterLen+4)) { 5866 sincTable[i+4] = sinc(cutoff, (i/cast(float)oversample-filterLen/2), filterLen, qualityMap.ptr[srQuality].windowFunc); 5867 } 5868 if (srQuality > 8) { 5869 resampler = &resamplerBasicInterpolate!double; 5870 } else { 5871 resampler = &resamplerBasicInterpolate!float; 5872 } 5873 } 5874 5875 /* Here's the place where we update the filter memory to take into account 5876 the change in filter length. It's probably the messiest part of the code 5877 due to handling of lots of corner cases. */ 5878 5879 // adding bufferSize to filterLen won't overflow here because filterLen could be multiplied by float.sizeof above 5880 minAllocSize = filterLen-1+bufferSize; 5881 if (minAllocSize > memAllocSize) { 5882 if (int.max/float.sizeof/chanCount < minAllocSize) goto fail; 5883 auto nslen = cast(uint)(chanCount*minAllocSize*mem[0].sizeof); 5884 if (nslen > realMemLen) { 5885 if (nslen < 16384) nslen = 16384; 5886 auto x = cast(float*)realloc(mem, nslen); 5887 if (x is null) goto fail; 5888 mem = x; 5889 realMemLen = nslen; 5890 } 5891 memAllocSize = minAllocSize; 5892 } 5893 if (!started) { 5894 //foreach (i=0;i<chanCount*memAllocSize;i++) mem[i] = 0; 5895 mem[0..chanCount*memAllocSize] = 0; 5896 } else if (filterLen > oldFilterLen) { 5897 // increase the filter length 5898 foreach_reverse (uint i; 0..chanCount) { 5899 uint j; 5900 uint olen = oldFilterLen; 5901 { 5902 // try and remove the magic samples as if nothing had happened 5903 //FIXME: this is wrong but for now we need it to avoid going over the array bounds 5904 olen = oldFilterLen+2*magicSamples.ptr[i]; 5905 for (j = oldFilterLen-1+magicSamples.ptr[i]; j--; ) mem[i*memAllocSize+j+magicSamples.ptr[i]] = mem[i*oldAllocSize+j]; 5906 //for (j = 0; j < magicSamples.ptr[i]; ++j) mem[i*memAllocSize+j] = 0; 5907 mem[i*memAllocSize..i*memAllocSize+magicSamples.ptr[i]] = 0; 5908 magicSamples.ptr[i] = 0; 5909 } 5910 if (filterLen > olen) { 5911 // if the new filter length is still bigger than the "augmented" length 5912 // copy data going backward 5913 for (j = 0; j < olen-1; ++j) mem[i*memAllocSize+(filterLen-2-j)] = mem[i*memAllocSize+(olen-2-j)]; 5914 // then put zeros for lack of anything better 5915 for (; j < filterLen-1; ++j) mem[i*memAllocSize+(filterLen-2-j)] = 0; 5916 // adjust lastSample 5917 lastSample.ptr[i] += (filterLen-olen)/2; 5918 } else { 5919 // put back some of the magic! 5920 magicSamples.ptr[i] = (olen-filterLen)/2; 5921 for (j = 0; j < filterLen-1+magicSamples.ptr[i]; ++j) mem[i*memAllocSize+j] = mem[i*memAllocSize+j+magicSamples.ptr[i]]; 5922 } 5923 } 5924 } else if (filterLen < oldFilterLen) { 5925 // reduce filter length, this a bit tricky 5926 // we need to store some of the memory as "magic" samples so they can be used directly as input the next time(s) 5927 foreach (immutable i; 0..chanCount) { 5928 uint j; 5929 uint oldMagic = magicSamples.ptr[i]; 5930 magicSamples.ptr[i] = (oldFilterLen-filterLen)/2; 5931 // we must copy some of the memory that's no longer used 5932 // copy data going backward 5933 for (j = 0; j < filterLen-1+magicSamples.ptr[i]+oldMagic; ++j) { 5934 mem[i*memAllocSize+j] = mem[i*memAllocSize+j+magicSamples.ptr[i]]; 5935 } 5936 magicSamples.ptr[i] += oldMagic; 5937 } 5938 } 5939 return Error.OK; 5940 5941 fail: 5942 resampler = null; 5943 /* mem may still contain consumed input samples for the filter. 5944 Restore filterLen so that filterLen-1 still points to the position after 5945 the last of these samples. */ 5946 filterLen = oldFilterLen; 5947 return Error.NoMemory; 5948 } 5949 } 5950 5951 5952 // ////////////////////////////////////////////////////////////////////////// // 5953 static immutable double[68] kaiser12Table = [ 5954 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, 5955 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, 5956 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601, 5957 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014, 5958 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490, 5959 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546, 5960 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178, 5961 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947, 5962 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058, 5963 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438, 5964 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734, 5965 0.00001000, 0.00000000]; 5966 5967 static immutable double[36] kaiser10Table = [ 5968 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, 5969 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, 5970 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962, 5971 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451, 5972 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739, 5973 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000]; 5974 5975 static immutable double[36] kaiser8Table = [ 5976 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, 5977 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, 5978 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272, 5979 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758, 5980 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, 5981 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000]; 5982 5983 static immutable double[36] kaiser6Table = [ 5984 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, 5985 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, 5986 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561, 5987 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058, 5988 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600, 5989 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000]; 5990 5991 struct FuncDef { 5992 immutable(double)* table; 5993 int oversample; 5994 } 5995 5996 static immutable FuncDef Kaiser12 = FuncDef(kaiser12Table.ptr, 64); 5997 static immutable FuncDef Kaiser10 = FuncDef(kaiser10Table.ptr, 32); 5998 static immutable FuncDef Kaiser8 = FuncDef(kaiser8Table.ptr, 32); 5999 static immutable FuncDef Kaiser6 = FuncDef(kaiser6Table.ptr, 32); 6000 6001 6002 struct QualityMapping { 6003 int baseLength; 6004 int oversample; 6005 float downsampleBandwidth; 6006 float upsampleBandwidth; 6007 immutable FuncDef* windowFunc; 6008 } 6009 6010 6011 /* This table maps conversion quality to internal parameters. There are two 6012 reasons that explain why the up-sampling bandwidth is larger than the 6013 down-sampling bandwidth: 6014 1) When up-sampling, we can assume that the spectrum is already attenuated 6015 close to the Nyquist rate (from an A/D or a previous resampling filter) 6016 2) Any aliasing that occurs very close to the Nyquist rate will be masked 6017 by the sinusoids/noise just below the Nyquist rate (guaranteed only for 6018 up-sampling). 6019 */ 6020 static immutable QualityMapping[11] qualityMap = [ 6021 QualityMapping( 8, 4, 0.830f, 0.860f, &Kaiser6 ), /* Q0 */ 6022 QualityMapping( 16, 4, 0.850f, 0.880f, &Kaiser6 ), /* Q1 */ 6023 QualityMapping( 32, 4, 0.882f, 0.910f, &Kaiser6 ), /* Q2 */ /* 82.3% cutoff ( ~60 dB stop) 6 */ 6024 QualityMapping( 48, 8, 0.895f, 0.917f, &Kaiser8 ), /* Q3 */ /* 84.9% cutoff ( ~80 dB stop) 8 */ 6025 QualityMapping( 64, 8, 0.921f, 0.940f, &Kaiser8 ), /* Q4 */ /* 88.7% cutoff ( ~80 dB stop) 8 */ 6026 QualityMapping( 80, 16, 0.922f, 0.940f, &Kaiser10), /* Q5 */ /* 89.1% cutoff (~100 dB stop) 10 */ 6027 QualityMapping( 96, 16, 0.940f, 0.945f, &Kaiser10), /* Q6 */ /* 91.5% cutoff (~100 dB stop) 10 */ 6028 QualityMapping(128, 16, 0.950f, 0.950f, &Kaiser10), /* Q7 */ /* 93.1% cutoff (~100 dB stop) 10 */ 6029 QualityMapping(160, 16, 0.960f, 0.960f, &Kaiser10), /* Q8 */ /* 94.5% cutoff (~100 dB stop) 10 */ 6030 QualityMapping(192, 32, 0.968f, 0.968f, &Kaiser12), /* Q9 */ /* 95.5% cutoff (~100 dB stop) 10 */ 6031 QualityMapping(256, 32, 0.975f, 0.975f, &Kaiser12), /* Q10 */ /* 96.6% cutoff (~100 dB stop) 10 */ 6032 ]; 6033 6034 6035 nothrow @trusted @nogc: 6036 /*8, 24, 40, 56, 80, 104, 128, 160, 200, 256, 320*/ 6037 double computeFunc (float x, immutable FuncDef* func) { 6038 //double[4] interp; 6039 float y = x*func.oversample; 6040 int ind = cast(int)lrintf(floor(y)); 6041 float frac = (y-ind); 6042 immutable f2 = frac*frac; 6043 immutable f3 = f2*frac; 6044 double interp3 = -0.1666666667*frac+0.1666666667*(f3); 6045 double interp2 = frac+0.5*(f2)-0.5*(f3); 6046 //double interp2 = 1.0f-0.5f*frac-f2+0.5f*f3; 6047 double interp0 = -0.3333333333*frac+0.5*(f2)-0.1666666667*(f3); 6048 // just to make sure we don't have rounding problems 6049 double interp1 = 1.0f-interp3-interp2-interp0; 6050 //sum = frac*accum[1]+(1-frac)*accum[2]; 6051 return interp0*func.table[ind]+interp1*func.table[ind+1]+interp2*func.table[ind+2]+interp3*func.table[ind+3]; 6052 } 6053 6054 6055 // the slow way of computing a sinc for the table; should improve that some day 6056 float sinc (float cutoff, float x, int N, immutable FuncDef *windowFunc) { 6057 version(LittleEndian) { 6058 align(1) union temp_float { align(1): float f; uint n; } 6059 } else { 6060 static T fabs(T) (T n) pure { return (n < 0 ? -n : n); } 6061 } 6062 version(LittleEndian) { 6063 temp_float txx = void; 6064 txx.f = x; 6065 txx.n &= 0x7fff_ffff; // abs 6066 if (txx.f < 1.0e-6f) return cutoff; 6067 if (txx.f > 0.5f*N) return 0; 6068 } else { 6069 if (fabs(x) < 1.0e-6f) return cutoff; 6070 if (fabs(x) > 0.5f*N) return 0; 6071 } 6072 //FIXME: can it really be any slower than this? 6073 immutable float xx = x*cutoff; 6074 immutable pixx = PI*xx; 6075 version(LittleEndian) { 6076 return cutoff*sin(pixx)/pixx*computeFunc(2.0*txx.f/N, windowFunc); 6077 } else { 6078 return cutoff*sin(pixx)/pixx*computeFunc(fabs(2.0*x/N), windowFunc); 6079 } 6080 } 6081 6082 6083 void cubicCoef (in float frac, float* interp) { 6084 immutable f2 = frac*frac; 6085 immutable f3 = f2*frac; 6086 // compute interpolation coefficients; i'm not sure whether this corresponds to cubic interpolation but I know it's MMSE-optimal on a sinc 6087 interp[0] = -0.16667f*frac+0.16667f*f3; 6088 interp[1] = frac+0.5f*f2-0.5f*f3; 6089 //interp[2] = 1.0f-0.5f*frac-f2+0.5f*f3; 6090 interp[3] = -0.33333f*frac+0.5f*f2-0.16667f*f3; 6091 // just to make sure we don't have rounding problems 6092 interp[2] = 1.0-interp[0]-interp[1]-interp[3]; 6093 } 6094 6095 6096 // ////////////////////////////////////////////////////////////////////////// // 6097 int resamplerBasicDirect(T) (ref OpusResampler st, uint chanIdx, const(float)* indata, uint* indataLen, float* outdata, uint* outdataLen) 6098 if (is(T == float) || is(T == double)) 6099 { 6100 auto N = st.filterLen; 6101 static if (is(T == double)) assert(N%4 == 0); 6102 int outSample = 0; 6103 int lastSample = st.lastSample.ptr[chanIdx]; 6104 uint sampFracNum = st.sampFracNum.ptr[chanIdx]; 6105 const(float)* sincTable = st.sincTable; 6106 immutable outStride = st.outStride; 6107 immutable intAdvance = st.intAdvance; 6108 immutable fracAdvance = st.fracAdvance; 6109 immutable denRate = st.denRate; 6110 T sum = void; 6111 while (!(lastSample >= cast(int)(*indataLen) || outSample >= cast(int)(*outdataLen))) { 6112 const(float)* sinct = &sincTable[sampFracNum*N]; 6113 const(float)* iptr = &indata[lastSample]; 6114 static if (is(T == float)) { 6115 // at least 2x speedup with SSE here (but for unrolled loop) 6116 if (N%4 == 0) 6117 { 6118 // no SSE; for my i3 unrolled loop is almost of the speed of SSE code 6119 T[4] accum = 0; 6120 foreach (immutable j; 0..N/4) { 6121 accum.ptr[0] += *sinct++ * *iptr++; 6122 accum.ptr[1] += *sinct++ * *iptr++; 6123 accum.ptr[2] += *sinct++ * *iptr++; 6124 accum.ptr[3] += *sinct++ * *iptr++; 6125 } 6126 sum = accum.ptr[0]+accum.ptr[1]+accum.ptr[2]+accum.ptr[3]; 6127 } 6128 else 6129 { 6130 sum = 0; 6131 foreach (immutable j; 0..N) sum += *sinct++ * *iptr++; 6132 } 6133 outdata[outStride*outSample++] = sum; 6134 } else { 6135 if (N%4 == 0) { 6136 //TODO: write SSE code here! 6137 // for my i3 unrolled loop is ~2 times faster 6138 T[4] accum = 0; 6139 foreach (immutable j; 0..N/4) { 6140 accum.ptr[0] += cast(double)*sinct++ * cast(double)*iptr++; 6141 accum.ptr[1] += cast(double)*sinct++ * cast(double)*iptr++; 6142 accum.ptr[2] += cast(double)*sinct++ * cast(double)*iptr++; 6143 accum.ptr[3] += cast(double)*sinct++ * cast(double)*iptr++; 6144 } 6145 sum = accum.ptr[0]+accum.ptr[1]+accum.ptr[2]+accum.ptr[3]; 6146 } else { 6147 sum = 0; 6148 foreach (immutable j; 0..N) sum += cast(double)*sinct++ * cast(double)*iptr++; 6149 } 6150 outdata[outStride*outSample++] = cast(float)sum; 6151 } 6152 lastSample += intAdvance; 6153 sampFracNum += fracAdvance; 6154 if (sampFracNum >= denRate) { 6155 sampFracNum -= denRate; 6156 ++lastSample; 6157 } 6158 } 6159 st.lastSample.ptr[chanIdx] = lastSample; 6160 st.sampFracNum.ptr[chanIdx] = sampFracNum; 6161 return outSample; 6162 } 6163 6164 6165 int resamplerBasicInterpolate(T) (ref OpusResampler st, uint chanIdx, const(float)* indata, uint *indataLen, float *outdata, uint *outdataLen) 6166 if (is(T == float) || is(T == double)) 6167 { 6168 immutable N = st.filterLen; 6169 assert(N%4 == 0); 6170 int outSample = 0; 6171 int lastSample = st.lastSample.ptr[chanIdx]; 6172 uint sampFracNum = st.sampFracNum.ptr[chanIdx]; 6173 immutable outStride = st.outStride; 6174 immutable intAdvance = st.intAdvance; 6175 immutable fracAdvance = st.fracAdvance; 6176 immutable denRate = st.denRate; 6177 float sum; 6178 6179 float[4] interp = void; 6180 T[4] accum = void; 6181 while (!(lastSample >= cast(int)(*indataLen) || outSample >= cast(int)(*outdataLen))) { 6182 const(float)* iptr = &indata[lastSample]; 6183 const int offset = sampFracNum*st.oversample/st.denRate; 6184 const float frac = (cast(float)((sampFracNum*st.oversample)%st.denRate))/st.denRate; 6185 accum[] = 0; 6186 //TODO: optimize! 6187 foreach (immutable j; 0..N) { 6188 immutable T currIn = iptr[j]; 6189 accum.ptr[0] += currIn*(st.sincTable[4+(j+1)*st.oversample-offset-2]); 6190 accum.ptr[1] += currIn*(st.sincTable[4+(j+1)*st.oversample-offset-1]); 6191 accum.ptr[2] += currIn*(st.sincTable[4+(j+1)*st.oversample-offset]); 6192 accum.ptr[3] += currIn*(st.sincTable[4+(j+1)*st.oversample-offset+1]); 6193 } 6194 6195 cubicCoef(frac, interp.ptr); 6196 sum = (interp.ptr[0]*accum.ptr[0])+(interp.ptr[1]*accum.ptr[1])+(interp.ptr[2]*accum.ptr[2])+(interp.ptr[3]*accum.ptr[3]); 6197 6198 outdata[outStride*outSample++] = sum; 6199 lastSample += intAdvance; 6200 sampFracNum += fracAdvance; 6201 if (sampFracNum >= denRate) { 6202 sampFracNum -= denRate; 6203 ++lastSample; 6204 } 6205 } 6206 6207 st.lastSample.ptr[chanIdx] = lastSample; 6208 st.sampFracNum.ptr[chanIdx] = sampFracNum; 6209 return outSample; 6210 } 6211 6212 6213 // ////////////////////////////////////////////////////////////////////////// // 6214 uint gcd (uint a, uint b) pure { 6215 if (a == 0) return b; 6216 if (b == 0) return a; 6217 for (;;) { 6218 if (a > b) { 6219 a %= b; 6220 if (a == 0) return b; 6221 if (a == 1) return 1; 6222 } else { 6223 b %= a; 6224 if (b == 0) return a; 6225 if (b == 1) return 1; 6226 } 6227 } 6228 } 6229 6230 6231 enum AV_SAMPLE_FMT_FLTP = 8; //HACK 6232 6233 6234 static immutable uint16_t[16] silk_frame_duration_ms = [ 6235 10, 20, 40, 60, 6236 10, 20, 40, 60, 6237 10, 20, 40, 60, 6238 10, 20, 6239 10, 20, 6240 ]; 6241 6242 /* number of samples of silence to feed to the resampler at the beginning */ 6243 static immutable int[5] silk_resample_delay = [ 4, 8, 11, 11, 11 ]; 6244 6245 static immutable uint8_t[5] celt_band_end = [ 13, 17, 17, 19, 21 ]; 6246 6247 static int get_silk_samplerate (int config) { 6248 return (config < 4 ? 8000 : config < 8 ? 12000 : 16000); 6249 } 6250 6251 /** 6252 * Range decoder 6253 */ 6254 static int opus_rc_init (OpusRangeCoder *rc, const(uint8_t)* data, int size) { 6255 //conwritefln!"size=%s; 0x%02x"(size, data[0]); 6256 int ret = rc.gb.init_get_bits8(data, size); 6257 if (ret < 0) return ret; 6258 6259 rc.range = 128; 6260 rc.value = 127 - rc.gb.get_bits(7); 6261 rc.total_read_bits = 9; 6262 opus_rc_normalize(rc); 6263 //conwriteln("range=", rc.range, "; value=", rc.value); 6264 //assert(0); 6265 6266 return 0; 6267 } 6268 6269 static void opus_raw_init (OpusRangeCoder* rc, const(uint8_t)* rightend, uint bytes) { 6270 rc.rb.position = rightend; 6271 rc.rb.bytes = bytes; 6272 rc.rb.cachelen = 0; 6273 rc.rb.cacheval = 0; 6274 } 6275 6276 static void opus_fade (float *out_, const(float)* in1, const(float)* in2, const(float)* window, int len) { 6277 for (int i = 0; i < len; i++) out_[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]); 6278 } 6279 6280 static int opus_flush_resample (OpusStreamContext* s, int nb_samples) { 6281 int celt_size = av_audio_fifo_size(s.celt_delay); //k8 6282 int ret, i; 6283 ret = s.flr.swrconvert(cast(float**)s.out_, nb_samples, null, 0); 6284 if (ret < 0) return AVERROR_BUG; 6285 if (ret != nb_samples) { 6286 //av_log(s.avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n", ret); 6287 return AVERROR_BUG; 6288 } 6289 6290 if (celt_size) { 6291 if (celt_size != nb_samples) { 6292 //av_log(s.avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n"); 6293 return AVERROR_BUG; 6294 } 6295 av_audio_fifo_read(s.celt_delay, cast(void**)s.celt_output.ptr, nb_samples); 6296 for (i = 0; i < s.output_channels; i++) { 6297 vector_fmac_scalar(s.out_[i], s.celt_output[i], 1.0, nb_samples); 6298 } 6299 } 6300 6301 if (s.redundancy_idx) { 6302 for (i = 0; i < s.output_channels; i++) { 6303 opus_fade(s.out_[i], s.out_[i], s.redundancy_output[i] + 120 + s.redundancy_idx, ff_celt_window2.ptr + s.redundancy_idx, 120 - s.redundancy_idx); 6304 } 6305 s.redundancy_idx = 0; 6306 } 6307 6308 s.out_[0] += nb_samples; 6309 s.out_[1] += nb_samples; 6310 s.out_size -= nb_samples * float.sizeof; 6311 6312 return 0; 6313 } 6314 6315 static int opus_init_resample (OpusStreamContext* s) { 6316 float[16] delay = 0.0; 6317 const(float)*[2] delayptr = [ cast(immutable(float)*)delay.ptr, cast(immutable(float)*)delay.ptr ]; 6318 float[128] odelay = void; 6319 float*[2] odelayptr = [ odelay.ptr, odelay.ptr ]; 6320 int ret; 6321 6322 if (s.flr.inited && s.flr.getInRate == s.silk_samplerate) { 6323 s.flr.reset(); 6324 } else if (!s.flr.inited || s.flr.getChans != s.output_channels) { 6325 // use Voip(3) quality 6326 if (s.flr.setup(s.output_channels, s.silk_samplerate, 48000, 3) != s.flr.Error.OK) return AVERROR_BUG; 6327 } else { 6328 if (s.flr.setRate(s.silk_samplerate, 48000) != s.flr.Error.OK) return AVERROR_BUG; 6329 } 6330 6331 ret = s.flr.swrconvert(odelayptr.ptr, 128, delayptr.ptr, silk_resample_delay[s.packet.bandwidth]); 6332 if (ret < 0) { 6333 //av_log(s.avctx, AV_LOG_ERROR, "Error feeding initial silence to the resampler.\n"); 6334 return AVERROR_BUG; 6335 } 6336 6337 return 0; 6338 } 6339 6340 static int opus_decode_redundancy (OpusStreamContext* s, const(uint8_t)* data, int size) { 6341 int ret; 6342 OpusBandwidth bw = s.packet.bandwidth; 6343 6344 if (s.packet.mode == OPUS_MODE_SILK && bw == OPUS_BANDWIDTH_MEDIUMBAND) bw = OPUS_BANDWIDTH_WIDEBAND; 6345 6346 ret = opus_rc_init(&s.redundancy_rc, data, size); 6347 if (ret < 0) goto fail; 6348 opus_raw_init(&s.redundancy_rc, data + size, size); 6349 6350 ret = ff_celt_decode_frame(s.celt, &s.redundancy_rc, s.redundancy_output.ptr, s.packet.stereo + 1, 240, 0, celt_band_end[s.packet.bandwidth]); 6351 if (ret < 0) goto fail; 6352 6353 return 0; 6354 fail: 6355 //av_log(s.avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n"); 6356 return ret; 6357 } 6358 6359 static int opus_decode_frame (OpusStreamContext* s, const(uint8_t)* data, int size) { 6360 int samples = s.packet.frame_duration; 6361 int redundancy = 0; 6362 int redundancy_size, redundancy_pos; 6363 int ret, i, consumed; 6364 int delayed_samples = s.delayed_samples; 6365 6366 ret = opus_rc_init(&s.rc, data, size); 6367 if (ret < 0) return ret; 6368 6369 //if (s.packet.mode != OPUS_MODE_CELT) assert(0); 6370 // decode the silk frame 6371 if (s.packet.mode == OPUS_MODE_SILK || s.packet.mode == OPUS_MODE_HYBRID) { 6372 if (!s.flr.inited) { 6373 ret = opus_init_resample(s); 6374 if (ret < 0) return ret; 6375 } 6376 6377 samples = ff_silk_decode_superframe(s.silk, &s.rc, s.silk_output.ptr, 6378 FFMIN(s.packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND), 6379 s.packet.stereo + 1, 6380 silk_frame_duration_ms[s.packet.config]); 6381 if (samples < 0) { 6382 return samples; 6383 } 6384 immutable insamples = samples; 6385 samples = s.flr.swrconvert(cast(float**)s.out_.ptr, s.packet.frame_duration, cast(const(float)**)s.silk_output.ptr, samples); 6386 if (samples < 0) { 6387 //av_log(s.avctx, AV_LOG_ERROR, "Error resampling SILK data.\n"); 6388 return samples; 6389 } 6390 //conwriteln("dcsamples: ", samples, "; outs=", s.packet.frame_duration, "; ins=", insamples); 6391 //k8???!!! assert((samples & 7) == 0); 6392 s.delayed_samples += s.packet.frame_duration - samples; 6393 } else { 6394 ff_silk_flush(s.silk); 6395 } 6396 6397 // decode redundancy information 6398 consumed = opus_rc_tell(&s.rc); 6399 if (s.packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8) redundancy = opus_rc_p2model(&s.rc, 12); 6400 else if (s.packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8) redundancy = 1; 6401 6402 if (redundancy) { 6403 redundancy_pos = opus_rc_p2model(&s.rc, 1); 6404 6405 if (s.packet.mode == OPUS_MODE_HYBRID) 6406 redundancy_size = opus_rc_unimodel(&s.rc, 256) + 2; 6407 else 6408 redundancy_size = size - (consumed + 7) / 8; 6409 size -= redundancy_size; 6410 if (size < 0) { 6411 //av_log(s.avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n"); 6412 return AVERROR_INVALIDDATA; 6413 } 6414 6415 if (redundancy_pos) { 6416 ret = opus_decode_redundancy(s, data + size, redundancy_size); 6417 if (ret < 0) return ret; 6418 ff_celt_flush(s.celt); 6419 } 6420 } 6421 6422 // decode the CELT frame 6423 if (s.packet.mode == OPUS_MODE_CELT || s.packet.mode == OPUS_MODE_HYBRID) { 6424 float*[2] out_tmp = [ s.out_[0], s.out_[1] ]; 6425 float **dst = (s.packet.mode == OPUS_MODE_CELT ? out_tmp.ptr : s.celt_output.ptr); 6426 int celt_output_samples = samples; 6427 int delay_samples = av_audio_fifo_size(s.celt_delay); 6428 6429 if (delay_samples) { 6430 if (s.packet.mode == OPUS_MODE_HYBRID) { 6431 av_audio_fifo_read(s.celt_delay, cast(void**)s.celt_output.ptr, delay_samples); 6432 6433 for (i = 0; i < s.output_channels; i++) { 6434 vector_fmac_scalar(out_tmp[i], s.celt_output[i], 1.0, delay_samples); 6435 out_tmp[i] += delay_samples; 6436 } 6437 celt_output_samples -= delay_samples; 6438 } else { 6439 //av_log(s.avctx, AV_LOG_WARNING, "Spurious CELT delay samples present.\n"); 6440 av_audio_fifo_drain(s.celt_delay, delay_samples); 6441 //if (s.avctx.err_recognition & AV_EF_EXPLODE) return AVERROR_BUG; 6442 } 6443 } 6444 6445 opus_raw_init(&s.rc, data + size, size); 6446 6447 ret = ff_celt_decode_frame(s.celt, &s.rc, dst, 6448 s.packet.stereo + 1, 6449 s.packet.frame_duration, 6450 (s.packet.mode == OPUS_MODE_HYBRID) ? 17 : 0, 6451 celt_band_end[s.packet.bandwidth]); 6452 if (ret < 0) return ret; 6453 6454 if (s.packet.mode == OPUS_MODE_HYBRID) { 6455 int celt_delay = s.packet.frame_duration - celt_output_samples; 6456 void*[2] delaybuf = [ s.celt_output[0] + celt_output_samples, 6457 s.celt_output[1] + celt_output_samples ]; 6458 6459 for (i = 0; i < s.output_channels; i++) { 6460 vector_fmac_scalar(out_tmp[i], s.celt_output[i], 1.0, celt_output_samples); 6461 } 6462 6463 ret = av_audio_fifo_write(s.celt_delay, delaybuf.ptr, celt_delay); 6464 if (ret < 0) return ret; 6465 } 6466 } else { 6467 ff_celt_flush(s.celt); 6468 } 6469 6470 if (s.redundancy_idx) { 6471 for (i = 0; i < s.output_channels; i++) { 6472 opus_fade(s.out_[i], s.out_[i], 6473 s.redundancy_output[i] + 120 + s.redundancy_idx, 6474 ff_celt_window2.ptr + s.redundancy_idx, 120 - s.redundancy_idx); 6475 } 6476 s.redundancy_idx = 0; 6477 } 6478 6479 if (redundancy) { 6480 if (!redundancy_pos) { 6481 ff_celt_flush(s.celt); 6482 ret = opus_decode_redundancy(s, data + size, redundancy_size); 6483 if (ret < 0) return ret; 6484 6485 for (i = 0; i < s.output_channels; i++) { 6486 opus_fade(s.out_[i] + samples - 120 + delayed_samples, 6487 s.out_[i] + samples - 120 + delayed_samples, 6488 s.redundancy_output[i] + 120, 6489 ff_celt_window2.ptr, 120 - delayed_samples); 6490 if (delayed_samples) 6491 s.redundancy_idx = 120 - delayed_samples; 6492 } 6493 } else { 6494 for (i = 0; i < s.output_channels; i++) { 6495 memcpy(s.out_[i] + delayed_samples, s.redundancy_output[i], 120 * float.sizeof); 6496 opus_fade(s.out_[i] + 120 + delayed_samples, 6497 s.redundancy_output[i] + 120, 6498 s.out_[i] + 120 + delayed_samples, 6499 ff_celt_window2.ptr, 120); 6500 } 6501 } 6502 } 6503 6504 return samples; 6505 } 6506 6507 static int opus_decode_subpacket (OpusStreamContext* s, const(uint8_t)* buf, int buf_size, float** out_, int out_size, int nb_samples) { 6508 int output_samples = 0; 6509 int flush_needed = 0; 6510 int i, j, ret; 6511 6512 s.out_[0] = out_[0]; 6513 s.out_[1] = out_[1]; 6514 s.out_size = out_size; 6515 6516 /* check if we need to flush the resampler */ 6517 if (s.flr.inited) { 6518 if (buf) { 6519 int64_t cur_samplerate = s.flr.getInRate; 6520 //av_opt_get_int(s.swr, "in_sample_rate", 0, &cur_samplerate); 6521 flush_needed = (s.packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s.silk_samplerate); 6522 } else { 6523 flush_needed = !!s.delayed_samples; 6524 } 6525 } 6526 6527 if (!buf && !flush_needed) 6528 return 0; 6529 6530 /* use dummy output buffers if the channel is not mapped to anything */ 6531 if (s.out_[0] is null || 6532 (s.output_channels == 2 && s.out_[1] is null)) { 6533 av_fast_malloc(cast(void**)&s.out_dummy, &s.out_dummy_allocated_size, s.out_size); 6534 if (!s.out_dummy) 6535 return AVERROR(ENOMEM); 6536 if (!s.out_[0]) 6537 s.out_[0] = s.out_dummy; 6538 if (!s.out_[1]) 6539 s.out_[1] = s.out_dummy; 6540 } 6541 6542 /* flush the resampler if necessary */ 6543 if (flush_needed) { 6544 ret = opus_flush_resample(s, s.delayed_samples); 6545 if (ret < 0) { 6546 //av_log(s.avctx, AV_LOG_ERROR, "Error flushing the resampler.\n"); 6547 return ret; 6548 } 6549 //swr_close(s.swr); 6550 s.flr.deinit(); 6551 output_samples += s.delayed_samples; 6552 s.delayed_samples = 0; 6553 6554 if (!buf) 6555 goto finish; 6556 } 6557 6558 /* decode all the frames in the packet */ 6559 for (i = 0; i < s.packet.frame_count; i++) { 6560 int size = s.packet.frame_size[i]; 6561 int samples = opus_decode_frame(s, buf + s.packet.frame_offset[i], size); 6562 6563 if (samples < 0) { 6564 //av_log(s.avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n"); 6565 //if (s.avctx.err_recognition & AV_EF_EXPLODE) return samples; 6566 6567 for (j = 0; j < s.output_channels; j++) 6568 memset(s.out_[j], 0, s.packet.frame_duration * float.sizeof); 6569 samples = s.packet.frame_duration; 6570 } 6571 output_samples += samples; 6572 6573 for (j = 0; j < s.output_channels; j++) 6574 s.out_[j] += samples; 6575 s.out_size -= samples * float.sizeof; 6576 } 6577 6578 finish: 6579 s.out_[0] = s.out_[1] = null; 6580 s.out_size = 0; 6581 6582 return output_samples; 6583 } 6584 6585 6586 // ////////////////////////////////////////////////////////////////////////// // 6587 int opus_decode_packet (/*AVCtx* avctx,*/ OpusContext* c, AVFrame* frame, int* got_frame_ptr, AVPacket* avpkt) { const(uint8_t)*buf = avpkt.data; 6588 int buf_size = avpkt.size; 6589 int coded_samples = 0; 6590 int decoded_samples = int.max; 6591 int delayed_samples = 0; 6592 int i, ret; 6593 6594 // calculate the number of delayed samples 6595 for (i = 0; i < c.nb_streams; i++) { 6596 OpusStreamContext *s = &c.streams[i]; 6597 s.out_[0] = s.out_[1] = null; 6598 delayed_samples = FFMAX(delayed_samples, s.delayed_samples+av_audio_fifo_size(c.sync_buffers[i])); 6599 } 6600 6601 // decode the header of the first sub-packet to find out the sample count 6602 if (buf !is null) { 6603 OpusPacket *pkt = &c.streams[0].packet; 6604 ret = ff_opus_parse_packet(pkt, buf, buf_size, c.nb_streams > 1); 6605 if (ret < 0) { 6606 //av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n"); 6607 return ret; 6608 } 6609 coded_samples += pkt.frame_count * pkt.frame_duration; 6610 c.streams[0].silk_samplerate = get_silk_samplerate(pkt.config); 6611 } 6612 6613 frame.nb_samples = coded_samples + delayed_samples; 6614 //conwriteln("frame samples: ", frame.nb_samples); 6615 6616 /* no input or buffered data => nothing to do */ 6617 if (!frame.nb_samples) { 6618 *got_frame_ptr = 0; 6619 return 0; 6620 } 6621 6622 /* setup the data buffers */ 6623 ret = ff_get_buffer(frame, 0); 6624 if (ret < 0) return ret; 6625 frame.nb_samples = 0; 6626 6627 memset(c.out_, 0, c.nb_streams*2*(*c.out_).sizeof); 6628 for (i = 0; i < c.in_channels; i++) { 6629 ChannelMap *map = &c.channel_maps[i]; 6630 //if (!map.copy) conwriteln("[", 2*map.stream_idx+map.channel_idx, "] = [", i, "]"); 6631 if (!map.copy) c.out_[2*map.stream_idx+map.channel_idx] = cast(float*)frame.extended_data[i]; 6632 } 6633 6634 // read the data from the sync buffers 6635 for (i = 0; i < c.nb_streams; i++) { 6636 float** out_ = c.out_+2*i; 6637 int sync_size = av_audio_fifo_size(c.sync_buffers[i]); 6638 6639 float[32] sync_dummy = void; 6640 int out_dummy = (!out_[0]) | ((!out_[1]) << 1); 6641 6642 if (!out_[0]) out_[0] = sync_dummy.ptr; 6643 if (!out_[1]) out_[1] = sync_dummy.ptr; 6644 if (out_dummy && sync_size > /*FF_ARRAY_ELEMS*/sync_dummy.length) return AVERROR_BUG; 6645 6646 ret = av_audio_fifo_read(c.sync_buffers[i], cast(void**)out_, sync_size); 6647 if (ret < 0) return ret; 6648 6649 if (out_dummy & 1) out_[0] = null; else out_[0] += ret; 6650 if (out_dummy & 2) out_[1] = null; else out_[1] += ret; 6651 6652 //conwriteln("ret=", ret); 6653 c.out_size[i] = cast(int)(frame.linesize[0]-ret*float.sizeof); 6654 } 6655 6656 // decode each sub-packet 6657 for (i = 0; i < c.nb_streams; i++) { 6658 OpusStreamContext *s = &c.streams[i]; 6659 if (i && buf) { 6660 ret = ff_opus_parse_packet(&s.packet, buf, buf_size, (i != c.nb_streams-1)); 6661 if (ret < 0) { 6662 //av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n"); 6663 return ret; 6664 } 6665 if (coded_samples != s.packet.frame_count * s.packet.frame_duration) { 6666 //av_log(avctx, AV_LOG_ERROR, "Mismatching coded sample count in substream %d.\n", i); 6667 return AVERROR_INVALIDDATA; 6668 } 6669 s.silk_samplerate = get_silk_samplerate(s.packet.config); 6670 } 6671 6672 ret = opus_decode_subpacket(&c.streams[i], buf, s.packet.data_size, c.out_+2*i, c.out_size[i], coded_samples); 6673 if (ret < 0) return ret; 6674 c.decoded_samples[i] = ret; 6675 decoded_samples = FFMIN(decoded_samples, ret); 6676 6677 buf += s.packet.packet_size; 6678 buf_size -= s.packet.packet_size; 6679 } 6680 6681 // buffer the extra samples 6682 for (i = 0; i < c.nb_streams; i++) { 6683 int buffer_samples = c.decoded_samples[i]-decoded_samples; 6684 if (buffer_samples) { 6685 float*[2] buff = [ c.out_[2 * i + 0] ? c.out_[2 * i + 0] : cast(float*)frame.extended_data[0], 6686 c.out_[2 * i + 1] ? c.out_[2 * i + 1] : cast(float*)frame.extended_data[0] ]; 6687 buff[0] += decoded_samples; 6688 buff[1] += decoded_samples; 6689 ret = av_audio_fifo_write(c.sync_buffers[i], cast(void**)buff.ptr, buffer_samples); 6690 if (ret < 0) return ret; 6691 } 6692 } 6693 6694 for (i = 0; i < c.in_channels; i++) { 6695 ChannelMap *map = &c.channel_maps[i]; 6696 // handle copied channels 6697 if (map.copy) { 6698 memcpy(frame.extended_data[i], frame.extended_data[map.copy_idx], frame.linesize[0]); 6699 } else if (map.silence) { 6700 memset(frame.extended_data[i], 0, frame.linesize[0]); 6701 } 6702 if (c.gain_i && decoded_samples > 0) { 6703 vector_fmul_scalar(cast(float*)frame.extended_data[i], cast(float*)frame.extended_data[i], c.gain, /*FFALIGN(decoded_samples, 8)*/decoded_samples); 6704 } 6705 } 6706 6707 //frame.nb_samples = decoded_samples; 6708 *got_frame_ptr = !!decoded_samples; 6709 6710 //return /*avpkt.size*/datasize; 6711 return decoded_samples; 6712 } 6713 6714 6715 void opus_decode_flush (OpusContext* c) { 6716 for (int i = 0; i < c.nb_streams; i++) { 6717 OpusStreamContext *s = &c.streams[i]; 6718 6719 memset(&s.packet, 0, s.packet.sizeof); 6720 s.delayed_samples = 0; 6721 6722 if (s.celt_delay) av_audio_fifo_drain(s.celt_delay, av_audio_fifo_size(s.celt_delay)); 6723 //swr_close(s.swr); 6724 s.flr.deinit(); 6725 6726 av_audio_fifo_drain(c.sync_buffers[i], av_audio_fifo_size(c.sync_buffers[i])); 6727 6728 ff_silk_flush(s.silk); 6729 ff_celt_flush(s.celt); 6730 } 6731 } 6732 6733 int opus_decode_close (OpusContext* c) { 6734 int i; 6735 6736 for (i = 0; i < c.nb_streams; i++) { 6737 OpusStreamContext *s = &c.streams[i]; 6738 6739 ff_silk_free(&s.silk); 6740 ff_celt_free(&s.celt); 6741 6742 av_freep(&s.out_dummy); 6743 s.out_dummy_allocated_size = 0; 6744 6745 av_audio_fifo_free(s.celt_delay); 6746 //swr_free(&s.swr); 6747 s.flr.deinit(); 6748 } 6749 6750 av_freep(&c.streams); 6751 6752 if (c.sync_buffers) { 6753 for (i = 0; i < c.nb_streams; i++) av_audio_fifo_free(c.sync_buffers[i]); 6754 } 6755 av_freep(&c.sync_buffers); 6756 av_freep(&c.decoded_samples); 6757 av_freep(&c.out_); 6758 av_freep(&c.out_size); 6759 6760 c.nb_streams = 0; 6761 6762 av_freep(&c.channel_maps); 6763 //av_freep(&c.fdsp); 6764 6765 return 0; 6766 } 6767 6768 int opus_decode_init (AVCtx* avctx, OpusContext* c, short cmtgain) { 6769 int ret, i, j; 6770 6771 avctx.sample_fmt = AV_SAMPLE_FMT_FLTP; 6772 avctx.sample_rate = 48000; 6773 6774 //c.fdsp = avpriv_float_dsp_alloc(0); 6775 //if (!c.fdsp) return AVERROR(ENOMEM); 6776 6777 // find out the channel configuration 6778 ret = ff_opus_parse_extradata(avctx, c, cmtgain); 6779 if (ret < 0) { 6780 av_freep(&c.channel_maps); 6781 //av_freep(&c.fdsp); 6782 return ret; 6783 } 6784 c.in_channels = avctx.channels; 6785 6786 //conwriteln("c.nb_streams=", c.nb_streams); 6787 //conwriteln("chans=", c.in_channels); 6788 // allocate and init each independent decoder 6789 c.streams = av_mallocz_array!(typeof(c.streams[0]))(c.nb_streams); 6790 c.out_ = av_mallocz_array!(typeof(c.out_[0]))(c.nb_streams * 2); 6791 c.out_size = av_mallocz_array!(typeof(c.out_size[0]))(c.nb_streams); 6792 c.sync_buffers = av_mallocz_array!(typeof(c.sync_buffers[0]))(c.nb_streams); 6793 c.decoded_samples = av_mallocz_array!(typeof(c.decoded_samples[0]))(c.nb_streams); 6794 if (c.streams is null || c.sync_buffers is null || c.decoded_samples is null || c.out_ is null || c.out_size is null) { 6795 c.nb_streams = 0; 6796 ret = AVERROR(ENOMEM); 6797 goto fail; 6798 } 6799 6800 for (i = 0; i < c.nb_streams; i++) { 6801 OpusStreamContext *s = &c.streams[i]; 6802 uint64_t layout; 6803 6804 s.output_channels = (i < c.nb_stereo_streams) ? 2 : 1; 6805 //conwriteln("stream #", i, "; chans: ", s.output_channels); 6806 6807 //s.avctx = avctx; 6808 6809 for (j = 0; j < s.output_channels; j++) { 6810 s.silk_output[j] = s.silk_buf[j].ptr; 6811 s.celt_output[j] = s.celt_buf[j].ptr; 6812 s.redundancy_output[j] = s.redundancy_buf[j].ptr; 6813 } 6814 6815 //s.fdsp = c.fdsp; 6816 layout = (s.output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; 6817 6818 /+ 6819 s.swr = swr_alloc(); 6820 if (!s.swr) goto fail; 6821 6822 /* 6823 av_opt_set_int(s.swr, "in_sample_fmt", avctx.sample_fmt, 0); 6824 av_opt_set_int(s.swr, "out_sample_fmt", avctx.sample_fmt, 0); 6825 av_opt_set_int(s.swr, "in_channel_layout", layout, 0); 6826 av_opt_set_int(s.swr, "out_channel_layout", layout, 0); 6827 av_opt_set_int(s.swr, "out_sample_rate", avctx.sample_rate, 0); 6828 av_opt_set_int(s.swr, "filter_size", 16, 0); 6829 */ 6830 +/ 6831 /* 6832 s.swr = swr_alloc_set_opts(null, 6833 layout, // out_ch_layout 6834 AV_SAMPLE_FMT_FLTP, // out_sample_fmt 6835 avctx.sample_rate, // out_sample_rate 6836 layout, // in_ch_layout 6837 AV_SAMPLE_FMT_FLTP, // in_sample_fmt 6838 avctx.sample_rate, // in_sample_rate 6839 0, null); 6840 6841 conwriteln("in_sample_fmt : ", avctx.sample_fmt); 6842 conwriteln("out_sample_fmt : ", avctx.sample_fmt); 6843 conwriteln("in_channel_layout : ", layout); 6844 conwriteln("out_channel_layout: ", layout); 6845 conwriteln("out_sample_rate : ", avctx.sample_rate); 6846 conwriteln("filter_size : ", 16); 6847 */ 6848 6849 ret = ff_silk_init(/*avctx, */&s.silk, s.output_channels); 6850 if (ret < 0) goto fail; 6851 6852 ret = ff_celt_init(/*avctx, */&s.celt, s.output_channels); 6853 if (ret < 0) goto fail; 6854 6855 s.celt_delay = av_audio_fifo_alloc(avctx.sample_fmt, s.output_channels, 1024); 6856 if (!s.celt_delay) { 6857 ret = AVERROR(ENOMEM); 6858 goto fail; 6859 } 6860 6861 c.sync_buffers[i] = av_audio_fifo_alloc(avctx.sample_fmt, s.output_channels, 32); 6862 if (!c.sync_buffers[i]) { 6863 ret = AVERROR(ENOMEM); 6864 goto fail; 6865 } 6866 } 6867 6868 return 0; 6869 fail: 6870 opus_decode_close(/*avctx*/c); 6871 return ret; 6872 } 6873 6874 6875 int opus_decode_init_ll (OpusContext* c) { 6876 int channels = 2; 6877 c.gain_i = 0; 6878 c.gain = 0; 6879 c.nb_streams = 1; 6880 c.nb_stereo_streams = 1; 6881 c.in_channels = channels; 6882 c.channel_maps = av_mallocz_array!(typeof(c.channel_maps[0]))(channels); 6883 if (c.channel_maps is null) return AVERROR(ENOMEM); 6884 c.channel_maps[0].stream_idx = 0; 6885 c.channel_maps[0].channel_idx = 0; 6886 c.channel_maps[1].stream_idx = 0; 6887 c.channel_maps[1].channel_idx = 1; 6888 6889 //conwriteln("c.nb_streams=", c.nb_streams); 6890 // allocate and init each independent decoder 6891 c.streams = av_mallocz_array!(typeof(c.streams[0]))(c.nb_streams); 6892 c.out_ = av_mallocz_array!(typeof(c.out_[0]))(c.nb_streams * 2); 6893 c.out_size = av_mallocz_array!(typeof(c.out_size[0]))(c.nb_streams); 6894 c.sync_buffers = av_mallocz_array!(typeof(c.sync_buffers[0]))(c.nb_streams); 6895 c.decoded_samples = av_mallocz_array!(typeof(c.decoded_samples[0]))(c.nb_streams); 6896 if (c.streams is null || c.sync_buffers is null || c.decoded_samples is null || c.out_ is null || c.out_size is null) { 6897 c.nb_streams = 0; 6898 opus_decode_close(c); 6899 return AVERROR(ENOMEM); 6900 } 6901 6902 foreach (immutable i; 0..c.nb_streams) { 6903 OpusStreamContext *s = &c.streams[i]; 6904 uint64_t layout; 6905 6906 s.output_channels = (i < c.nb_stereo_streams ? 2 : 1); 6907 //conwriteln("stream #", i, "; chans: ", s.output_channels); 6908 6909 foreach (immutable j; 0..s.output_channels) { 6910 s.silk_output[j] = s.silk_buf[j].ptr; 6911 s.celt_output[j] = s.celt_buf[j].ptr; 6912 s.redundancy_output[j] = s.redundancy_buf[j].ptr; 6913 } 6914 6915 layout = (s.output_channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; 6916 6917 /+ 6918 s.swr = swr_alloc_set_opts(null, 6919 layout, // out_ch_layout 6920 AV_SAMPLE_FMT_FLTP, // out_sample_fmt 6921 48000, // out_sample_rate 6922 layout, // in_ch_layout 6923 AV_SAMPLE_FMT_FLTP, // in_sample_fmt 6924 48000, // in_sample_rate 6925 0, null); 6926 +/ 6927 6928 if (ff_silk_init(/*avctx, */&s.silk, s.output_channels) < 0) { 6929 opus_decode_close(c); 6930 return AVERROR(ENOMEM); 6931 } 6932 6933 if (ff_celt_init(/*avctx, */&s.celt, s.output_channels) < 0) { 6934 opus_decode_close(c); 6935 return AVERROR(ENOMEM); 6936 } 6937 6938 s.celt_delay = av_audio_fifo_alloc(AV_SAMPLE_FMT_FLTP, s.output_channels, 1024); 6939 if (!s.celt_delay) { 6940 opus_decode_close(c); 6941 return AVERROR(ENOMEM); 6942 } 6943 6944 c.sync_buffers[i] = av_audio_fifo_alloc(AV_SAMPLE_FMT_FLTP, s.output_channels, 32); 6945 if (!c.sync_buffers[i]) { 6946 opus_decode_close(c); 6947 return AVERROR(ENOMEM); 6948 } 6949 } 6950 6951 return 0; 6952 } 6953 6954 // ////////////////////////////////////////////////////////////////////////// // 6955 struct OggStream { 6956 private: 6957 6958 @nogc nothrow: 6959 enum MaxPageSize = 65025+Offsets.Lacing+255; 6960 //pragma(msg, MaxPageSize); // 65307 bytes 6961 //enum MaxPageSize = 65536; 6962 6963 // Ogg header entry offsets 6964 enum Offsets { 6965 Capture = 0, 6966 Version = 4, 6967 Flags = 5, 6968 Granulepos = 6, 6969 Serialno = 14, 6970 Sequenceno = 18, 6971 Crc = 22, 6972 Segments = 26, 6973 Lacing = 27, 6974 } 6975 6976 private: 6977 IOCallbacks* _io; 6978 void* _userData; 6979 // VFile fl; 6980 //ubyte[] buf; 6981 ubyte[65536*2] buf; 6982 uint bufpos, bufused; 6983 uint serno, seqno; 6984 bool eofhit; // "end-of-stream" hit 6985 long logStreamSize; 6986 ulong bytesRead; 6987 ulong newpos; 6988 long firstpagepos; 6989 long firstdatapgofs = -1; 6990 ulong firstgranule; 6991 6992 // current page info 6993 bool pgbos, pgeos, pgcont; 6994 ulong pggranule; 6995 ubyte segments; 6996 uint pgseqno, pgserno; 6997 uint pglength, pgdatalength; 6998 ubyte[255] seglen; 6999 uint curseg; // for packet reader 7000 7001 PageInfo lastpage; 7002 7003 public: 7004 bool packetBos; 7005 bool packetEos; 7006 bool packetBop; // first packet in page? 7007 bool packetEop; // last packet in page? 7008 ulong packetGranule; 7009 Vec!ubyte packetData; 7010 uint packetLength; 7011 7012 private: 7013 7014 // Extends I/O callbakcs 7015 void[] rawRead (void[] buf) nothrow 7016 { 7017 int bytesRead = _io.read(buf.ptr, cast(int)buf.length, _userData); 7018 return buf[0..bytesRead]; 7019 } 7020 7021 void moveBuf () nothrow 7022 { 7023 if (bufpos >= bufused) { bufpos = bufused = 0; return; } 7024 if (bufpos > 0) { 7025 memmove(buf.ptr, buf.ptr+bufpos, bufused-bufpos); 7026 bufused -= bufpos; 7027 bufpos = 0; 7028 } 7029 } 7030 7031 bool ensureBytes (uint len) nothrow 7032 { 7033 if (len > buf.length) 7034 assert(0, "internal OggStream error"); 7035 if (bufused-bufpos >= len) return true; 7036 if (eofhit) return false; 7037 // shift bytes 7038 if (bufused-bufpos > 0) { 7039 memmove(buf.ptr, buf.ptr+bufpos, bufused-bufpos); 7040 bufused -= bufpos; 7041 bufpos = 0; 7042 } else { 7043 bufused = bufpos = 0; 7044 } 7045 assert(bufpos == 0); 7046 assert(bufused < len); 7047 while (bufused < len) 7048 { 7049 auto rd = rawRead(buf[bufused..len]); 7050 if (rd.length == 0) { eofhit = true; return false; } 7051 bufused += cast(uint)rd.length; 7052 } 7053 return true; 7054 } 7055 7056 bool parsePageHeader () nothrow 7057 { 7058 if (!ensureBytes(Offsets.Lacing)) return false; 7059 if (!ensureBytes(Offsets.Lacing+buf.ptr[bufpos+Offsets.Segments])) return false; 7060 if (bufpos >= bufused) return false; 7061 auto p = (cast(const(ubyte)*)buf.ptr)+bufpos; 7062 if (p[0] != 'O' || p[1] != 'g' || p[2] != 'g' || p[3] != 'S') return false; 7063 if (p[Offsets.Version] != 0) return false; 7064 ubyte flags = p[Offsets.Flags]; 7065 if ((flags&~0x07) != 0) return false; 7066 ulong grpos = getMemInt!ulong(p+Offsets.Granulepos); 7067 uint serialno = getMemInt!uint(p+Offsets.Serialno); 7068 uint sequenceno = getMemInt!uint(p+Offsets.Sequenceno); 7069 uint crc = getMemInt!uint(p+Offsets.Crc); 7070 ubyte segcount = p[Offsets.Segments]; 7071 if (!ensureBytes(Offsets.Lacing+segcount)) return false; 7072 p = (cast(const(ubyte)*)buf.ptr)+bufpos; 7073 // calculate page size 7074 uint len = Offsets.Lacing+segcount; 7075 foreach (ubyte b; p[Offsets.Lacing..Offsets.Lacing+segcount]) len += b; 7076 if (!ensureBytes(len)) return false; // alas, invalid page 7077 //conwriteln("len=", len); 7078 p = (cast(const(ubyte)*)buf.ptr)+bufpos; 7079 // check page crc 7080 uint newcrc = crc32(p[0..Offsets.Crc]); 7081 ubyte[4] zeroes = 0; 7082 newcrc = crc32(zeroes[], newcrc); // per spec 7083 newcrc = crc32(p[Offsets.Crc+4..len], newcrc); 7084 if (newcrc != crc) return false; // bad crc 7085 // setup values for valid page 7086 pgcont = (flags&0x01 ? true : false); 7087 pgbos = (flags&0x02 ? true : false); 7088 pgeos = (flags&0x04 ? true : false); 7089 segments = segcount; 7090 if (segcount) seglen[0..segcount] = p[Offsets.Lacing..Offsets.Lacing+segcount]; 7091 pggranule = grpos; 7092 pgseqno = sequenceno; 7093 pgserno = serialno; 7094 pglength = len; 7095 pgdatalength = len-Offsets.Lacing-segcount; 7096 return true; 7097 } 7098 7099 long getfpos () nothrow 7100 { 7101 return _io.tell(_userData) - bufused + bufpos; 7102 } 7103 7104 // scan for page 7105 bool nextPage(bool first, bool ignoreseqno=false) (long maxbytes=long.max) nothrow 7106 { 7107 if (eofhit) return false; 7108 //Exceptions removed, so this shouldn't happen anymore 7109 //scope(failure) eofhit = true; 7110 curseg = 0; 7111 static if (!first) bufpos += pglength; // skip page data 7112 clearPage(); 7113 while (maxbytes >= Offsets.Lacing) { 7114 while (bufpos >= bufused || bufused-bufpos < 4) { 7115 if (eofhit) break; 7116 if (bufpos < bufused) { 7117 memmove(buf.ptr, buf.ptr+bufpos, bufused-bufpos); 7118 bufused -= bufpos; 7119 bufpos = 0; 7120 } else { 7121 bufpos = bufused = 0; 7122 } 7123 assert(bufused <= MaxPageSize); 7124 uint rdx = MaxPageSize-bufused; 7125 if (rdx > maxbytes) rdx = cast(uint)maxbytes; 7126 auto rd = rawRead(buf[bufused..bufused+rdx]); 7127 if (rd.length == 0) break; 7128 bufused += cast(uint)rd.length; 7129 maxbytes -= cast(uint)rd.length; 7130 } 7131 if (bufpos >= bufused || bufused-bufpos < 4) { eofhit = true; return false; } 7132 uint bleft = bufused-bufpos; 7133 auto b = (cast(const(ubyte)*)buf.ptr)+bufpos; 7134 while (bleft >= 4) { 7135 if (b[0] == 'O' && b[1] == 'g' && b[2] == 'g' && b[3] == 'S') { 7136 bufpos = bufused-bleft; 7137 if (parsePageHeader()) { 7138 //conwriteln("1: bufpos=", bufpos, "; bufused=", bufused, "; segs: ", seglen[0..segments], "; pgseqno=", pgseqno, "; seqno=", seqno, "; pgserno=", pgserno, "; serno=", serno); 7139 eofhit = pgeos; 7140 static if (first) { 7141 firstpagepos = _io.tell(_userData)-bufused+bufpos; 7142 firstdatapgofs = (pggranule && pggranule != -1 ? firstpagepos : -1); 7143 firstgranule = pggranule; 7144 serno = pgserno; 7145 seqno = pgseqno; 7146 return true; 7147 } else { 7148 if (serno == pgserno) { 7149 static if (!ignoreseqno) { 7150 bool ok = (seqno+1 == pgseqno); 7151 if (ok) ++seqno; 7152 } else { 7153 enum ok = true; 7154 } 7155 if (ok) { 7156 if (firstdatapgofs == -1 && pggranule && pggranule != -1) { 7157 firstdatapgofs = _io.tell(_userData)-bufused+bufpos; 7158 firstgranule = pggranule; 7159 } 7160 return true; 7161 } 7162 // alas 7163 static if (!ignoreseqno) { 7164 eofhit = true; 7165 return false; 7166 } 7167 } 7168 } 7169 // continue 7170 } else { 7171 if (eofhit) return false; 7172 } 7173 bleft = bufused-bufpos; 7174 b = (cast(const(ubyte)*)buf.ptr)+bufpos; 7175 } 7176 ++b; 7177 --bleft; 7178 } 7179 bufpos = bufused; 7180 } 7181 return false; 7182 } 7183 7184 void clearPage () nothrow 7185 { 7186 pgbos = pgeos = pgcont = false; 7187 pggranule = 0; 7188 segments = 0; 7189 pgseqno = pgserno = 0; 7190 pglength = pgdatalength = 0; 7191 seglen[] = 0; 7192 } 7193 7194 void clearPacket () nothrow { 7195 packetBos = packetBop = packetEop = packetEos = false; 7196 packetGranule = 0; 7197 packetData.fill(0); 7198 packetLength = 0; 7199 } 7200 7201 public: 7202 void close () nothrow { 7203 _io = null; 7204 lastpage = lastpage.init; 7205 bufpos = bufused = 0; 7206 curseg = 0; 7207 bytesRead = 0; 7208 eofhit = true; 7209 firstpagepos = 0; 7210 bytesRead = newpos = 0; 7211 logStreamSize = -1; 7212 clearPage(); 7213 clearPacket(); 7214 } 7215 7216 void setup (IOCallbacks* io, void* userData, bool* err) nothrow { 7217 close(); 7218 _io = io; 7219 _userData = userData; 7220 eofhit = false; 7221 bool didThrow; 7222 if (!nextPage!true()) goto errored; // can't find valid OGG pages 7223 if (pgcont || !pgbos) goto errored; // invalid starting Ogg page 7224 if (!loadPacket(&didThrow)) goto errored; // can't load Ogg packet 7225 if (didThrow) goto errored; 7226 7227 *err = false; 7228 return; 7229 7230 errored: 7231 close(); 7232 *err = true; 7233 return; 7234 7235 } 7236 7237 static struct PageInfo { 7238 uint seqnum; 7239 ulong granule; 7240 long pgfpos = -1; 7241 } 7242 7243 bool findLastPage (out PageInfo pi) nothrow 7244 { 7245 if (lastpage.pgfpos >= 0) { 7246 pi = lastpage; 7247 return true; 7248 } 7249 enum ChunkSize = 65535; 7250 moveBuf(); 7251 assert(buf.length-bufused >= ChunkSize); 7252 auto lastfpos = _io.tell(_userData); 7253 scope(success) 7254 { 7255 _io.seek(lastfpos, false, _userData); 7256 7257 // Note: seek error ignored above 7258 } 7259 auto flsize = _io.getFileLength(_userData); 7260 if (flsize < 0) return false; 7261 // linear scan backward 7262 auto flpos = flsize-firstpagepos-ChunkSize; 7263 if (flpos < firstpagepos) flpos = firstpagepos; 7264 for (;;) { 7265 if (!_io.seek(flpos, false, _userData)) 7266 return false; 7267 uint bulen = (flpos+ChunkSize <= flsize ? ChunkSize : cast(uint)(flsize-flpos)); 7268 if (bulen < 27) break; 7269 { 7270 auto read = rawRead(buf[bufused..bufused+bulen]); 7271 if (read.length != bulen) 7272 return false; 7273 } 7274 uint pos = bufused+bulen-27; 7275 uint pend = bufused+bulen; 7276 for (;;) { 7277 if (buf.ptr[pos] == 'O' && buf.ptr[pos+1] == 'g' && buf.ptr[pos+2] == 'g' && buf.ptr[pos+3] == 'S') { 7278 ulong gran = getMemInt!ulong(buf.ptr+pos+Offsets.Granulepos); 7279 if (gran > 0 && gran != -1 && buf.ptr[pos+Offsets.Version] == 0 && getMemInt!uint(buf.ptr+pos+Offsets.Serialno) == serno) { 7280 // ok, possible page found 7281 bool rereadbuf = false; 7282 auto opos = pos; 7283 // calc page size 7284 ubyte segs = buf.ptr[pos+Offsets.Segments]; 7285 uint pgsize = Offsets.Lacing+segs; 7286 ubyte[4] zeroes = 0; 7287 ubyte* p; 7288 uint newcrc; 7289 if (pend-pos < pgsize) { 7290 // load page 7291 pos = pend = bufused; 7292 rereadbuf = true; 7293 if (!_io.seek(flpos+opos-bufused, false, _userData)) 7294 return false; 7295 for (uint bp = 0; bp < MaxPageSize; ) { 7296 auto rd = rawRead(buf.ptr[pos+bp..pos+MaxPageSize]); 7297 if (rd.length == 0) { 7298 if (bp < pgsize) goto badpage; 7299 break; 7300 } 7301 bp += cast(uint)rd.length; 7302 pend += cast(uint)rd.length; 7303 } 7304 } 7305 foreach (ubyte ss; buf.ptr[pos+Offsets.Lacing..pos+Offsets.Lacing+segs]) pgsize += ss; 7306 if (pend-pos < pgsize) { 7307 // load page 7308 pos = bufused; 7309 rereadbuf = true; 7310 if (!_io.seek(flpos+opos-bufused, false, _userData)) 7311 return false; 7312 for (uint bp = 0; bp < MaxPageSize; ) { 7313 auto rd = rawRead(buf.ptr[pos+bp..pos+MaxPageSize]); 7314 if (rd.length == 0) { 7315 if (bp < pgsize) goto badpage; 7316 break; 7317 } 7318 bp += cast(uint)rd.length; 7319 pend += cast(uint)rd.length; 7320 } 7321 } 7322 // check page CRC 7323 p = buf.ptr+pos; 7324 newcrc = crc32(p[0..Offsets.Crc]); 7325 newcrc = crc32(zeroes[], newcrc); // per spec 7326 newcrc = crc32(p[Offsets.Crc+4..pgsize], newcrc); 7327 if (newcrc != getMemInt!uint(p+Offsets.Crc)) goto badpage; 7328 pi.seqnum = getMemInt!uint(p+Offsets.Sequenceno); 7329 pi.granule = gran; 7330 pi.pgfpos = flpos+opos-bufused; 7331 lastpage = pi; 7332 return true; 7333 badpage: 7334 if (rereadbuf) { 7335 if (!_io.seek(flpos, false, _userData)) 7336 return false; 7337 auto sliceOut = rawRead(buf[bufused..bufused+ChunkSize]); 7338 if (sliceOut.length != ChunkSize) 7339 return false; // bad Parsing 7340 pos = opos; 7341 pend = bufused+ChunkSize; 7342 } 7343 } 7344 } 7345 if (pos == bufused) break; // prev chunk 7346 --pos; 7347 } 7348 if (flpos == firstpagepos) break; // not found 7349 flpos -= ChunkSize-30; 7350 if (flpos < firstpagepos) flpos = firstpagepos; 7351 } 7352 return false; 7353 } 7354 7355 // end of stream? 7356 bool eos () const pure nothrow @safe @nogc { return eofhit; } 7357 7358 // logical beginning of stream? 7359 bool bos () const pure nothrow @safe @nogc { return pgbos; } 7360 7361 7362 // Not sure what this does, originally the code would throw exception or return a bool, that makes it three-stated, and it 7363 // seemingly didn't serve a purpose to do so. But for maximum safety we maintained that, perhaps it throw if it looses stream 7364 // sync somehow. The devil you know. 7365 bool loadPacket (bool* didThrow) nothrow 7366 { 7367 *didThrow = false; 7368 packetLength = 0; 7369 packetBos = pgbos; 7370 packetEos = pgeos; 7371 packetGranule = pggranule; 7372 packetBop = (curseg == 0); 7373 if (curseg >= segments) { 7374 if (!nextPage!false()) return false; 7375 if (pgcont || pgbos) 7376 { 7377 // invalid starting Ogg page 7378 *didThrow = true; 7379 return false; 7380 } 7381 packetBos = pgbos; 7382 packetBop = true; 7383 packetGranule = pggranule; 7384 } 7385 for (;;) { 7386 uint copyofs = bufpos+Offsets.Lacing+segments; 7387 foreach (ubyte psz; seglen[0..curseg]) copyofs += psz; 7388 uint copylen = 0; 7389 bool endofpacket = false; 7390 while (!endofpacket && curseg < segments) { 7391 copylen += seglen[curseg]; 7392 endofpacket = (seglen[curseg++] < 255); 7393 } 7394 //conwriteln("copyofs=", copyofs, "; copylen=", copylen, "; eop=", eop, "; packetLength=", packetLength, "; segments=", segments, "; curseg=", curseg); 7395 if (copylen > 0) { 7396 if (packetLength+copylen > 1024*1024*32) 7397 { 7398 // Ogg packet too big 7399 *didThrow = true; 7400 return false; 7401 } 7402 if (packetLength+copylen > packetData.length) 7403 { 7404 packetData.resize(packetLength+copylen); 7405 } 7406 memcpy(&packetData[packetLength], &buf[copyofs], copylen); 7407 //packetData[packetLength..packetLength+copylen] = buf.ptr[copyofs..copyofs+copylen]; 7408 packetLength += copylen; 7409 } 7410 if (endofpacket) { 7411 packetEop = (curseg >= segments); 7412 packetEos = pgeos; 7413 return true; 7414 } 7415 assert(curseg >= segments); 7416 // get next page 7417 if (!nextPage!false()) return false; 7418 if (!pgcont || pgbos) 7419 { 7420 // invalid cont Ogg page 7421 *didThrow = true; 7422 return false; 7423 } 7424 } 7425 } 7426 7427 // rescales the number x from the range of [0,from] to [0,to] x is in the range [0,from] from, to are in the range [1, 1<<62-1] 7428 static long rescale64 (long x, long from, long to) nothrow pure 7429 { 7430 if (x >= from) return to; 7431 if (x <= 0) return 0; 7432 7433 long frac = 0; 7434 long ret = 0; 7435 7436 foreach (immutable _; 0..64) { 7437 if (x >= from) { frac |= 1; x -= from; } 7438 x <<= 1; 7439 frac <<= 1; 7440 } 7441 7442 foreach (immutable _; 0..64) { 7443 if (frac&1) ret += to; 7444 frac >>= 1; 7445 ret >>= 1; 7446 } 7447 7448 return ret; 7449 } 7450 7451 /* Page granularity seek (faster than sample granularity because we 7452 don't do the last bit of decode to find a specific sample). 7453 7454 Seek to the last [granule marked] page preceding the specified pos 7455 location, such that decoding past the returned point will quickly 7456 arrive at the requested position. */ 7457 // return PCM (granule) position for loaded packet 7458 public long seekPCM (long pos, bool* didThrow) nothrow { 7459 enum ChunkSize = 65535; 7460 *didThrow = false; 7461 eofhit = false; 7462 7463 if (pos < 0) return -1; 7464 if (pos <= firstgranule) { 7465 bufused = bufpos = 0; 7466 pglength = 0; 7467 curseg = 0; 7468 if (!_io.seek(firstpagepos, false, _userData)) 7469 { 7470 *didThrow = true; 7471 return -1; 7472 } 7473 eofhit = false; 7474 if (!nextPage!true()) 7475 { 7476 *didThrow = true; 7477 return -1; 7478 } 7479 if (pgcont || !pgbos) 7480 { 7481 *didThrow = true; 7482 return -1; 7483 } 7484 for (;;) { 7485 if (pggranule && pggranule != -1) { 7486 curseg = 0; 7487 //for (int p = 0; p < segments; ++p) if (seglen[p] < 255) curseg = p+1; 7488 //auto rtg = pggranule; 7489 if (!loadPacket(didThrow)) 7490 if (*didThrow) return -1; 7491 return 0; 7492 } 7493 if (!nextPage!false()) 7494 { 7495 *didThrow = true; 7496 return -1; 7497 } 7498 } 7499 } 7500 7501 if (lastpage.pgfpos < 0) { 7502 PageInfo pi; 7503 if (!findLastPage(pi)) 7504 { 7505 *didThrow = true; 7506 return -1; 7507 } 7508 } 7509 7510 if (firstdatapgofs < 0) assert(0, "internal error"); 7511 7512 if (pos > lastpage.granule) pos = lastpage.granule; 7513 7514 //if (buf.length < ChunkSize) buf.length = ChunkSize; 7515 7516 long total = lastpage.granule; 7517 7518 long end = lastpage.pgfpos; 7519 long begin = firstdatapgofs; 7520 long begintime = 0/*firstgranule*/; 7521 long endtime = lastpage.granule; 7522 long target = pos;//-total+begintime; 7523 long best = -1; 7524 bool got_page = false; 7525 7526 // if we have only one page, there will be no bisection: grab the page here 7527 if (begin == end) { 7528 bufused = bufpos = 0; 7529 pglength = 0; 7530 curseg = 0; 7531 if (!_io.seek(begin, false, _userData)) 7532 { 7533 *didThrow = true; 7534 return -1; 7535 } 7536 eofhit = false; 7537 if (!nextPage!false()) return false; 7538 if (!loadPacket(didThrow)) return false; 7539 if (*didThrow) 7540 return -1; 7541 return true; 7542 } 7543 7544 // bisection loop 7545 while (begin < end) { 7546 long bisect; 7547 7548 if (end-begin < ChunkSize) { 7549 bisect = begin; 7550 } else { 7551 // take a (pretty decent) guess 7552 bisect = begin+rescale64(target-begintime, endtime-begintime, end-begin)-ChunkSize; 7553 if (bisect < begin+ChunkSize) bisect = begin; 7554 } 7555 7556 bufused = bufpos = 0; 7557 pglength = 0; 7558 curseg = 0; 7559 if (!_io.seek(bisect, false, _userData)) 7560 { 7561 *didThrow = true; 7562 return -1; 7563 } 7564 eofhit = false; 7565 7566 // read loop within the bisection loop 7567 while (begin < end) { 7568 // hack for nextpage 7569 if (!nextPage!(false, true)(end - getfpos)) { 7570 // there is no next page! 7571 if (bisect <= begin+1) { 7572 // no bisection left to perform: we've either found the best candidate already or failed; exit loop 7573 end = begin; 7574 } else { 7575 // we tried to load a fraction of the last page; back up a bit and try to get the whole last page 7576 if (bisect == 0) 7577 { 7578 *didThrow = true; 7579 return -1; 7580 } 7581 bisect -= ChunkSize; 7582 7583 // don't repeat/loop on a read we've already performed 7584 if (bisect <= begin) bisect = begin+1; 7585 7586 // seek and continue bisection 7587 bufused = bufpos = 0; 7588 pglength = 0; 7589 curseg = 0; 7590 if (! _io.seek(bisect, false, _userData)) 7591 { 7592 *didThrow = true; 7593 return -1; 7594 } 7595 } 7596 } else { 7597 //conwriteln("page #", pgseqno, " (", pggranule, ") at ", getfpos); 7598 long granulepos; 7599 got_page = true; 7600 7601 // got a page: analyze it 7602 // only consider pages from primary vorbis stream 7603 if (pgserno != serno) continue; 7604 7605 // only consider pages with the granulepos set 7606 granulepos = pggranule; 7607 if (granulepos == -1) continue; 7608 //conwriteln("pos=", pos, "; gran=", granulepos, "; target=", target); 7609 7610 if (granulepos < target) { 7611 // this page is a successful candidate! Set state 7612 best = getfpos; // raw offset of packet with granulepos 7613 begin = getfpos+pglength; // raw offset of next page 7614 begintime = granulepos; 7615 7616 // if we're before our target but within a short distance, don't bisect; read forward 7617 if (target-begintime > 48000) break; 7618 7619 bisect = begin; // *not* begin+1 as above 7620 } else { 7621 // this is one of our pages, but the granpos is post-target; it is not a bisection return candidate 7622 // the only way we'd use it is if it's the first page in the stream; we handle that case later outside the bisection 7623 if (bisect <= begin+1) { 7624 // no bisection left to perform: we've either found the best candidate already or failed; exit loop 7625 end = begin; 7626 } else { 7627 if (end == getfpos+pglength) { 7628 // bisection read to the end; use the known page boundary (result) to update bisection, back up a little bit, and try again 7629 end = getfpos; 7630 bisect -= ChunkSize; 7631 if (bisect <= begin) bisect = begin+1; 7632 bufused = bufpos = 0; 7633 pglength = 0; 7634 curseg = 0; 7635 if (! _io.seek(bisect, false, _userData)) 7636 { 7637 *didThrow = true; 7638 return -1; 7639 } 7640 eofhit = false; 7641 } else { 7642 // normal bisection 7643 end = bisect; 7644 endtime = granulepos; 7645 break; 7646 } 7647 } 7648 } 7649 } 7650 } 7651 } 7652 7653 // out of bisection: did it 'fail?' 7654 if (best == -1) { 7655 bufused = bufpos = 0; 7656 pglength = 0; 7657 curseg = 0; 7658 if (!_io.seek(firstpagepos, false, _userData)) 7659 { 7660 *didThrow = true; 7661 return -1; 7662 } 7663 eofhit = false; 7664 if (!nextPage!true()) 7665 { 7666 *didThrow = true; 7667 return -1; 7668 } 7669 if (pgcont || !pgbos) 7670 { 7671 *didThrow = true; 7672 return -1; 7673 } 7674 for (;;) { 7675 if (pggranule && pggranule != -1) 7676 { 7677 curseg = 0; 7678 if (!loadPacket(didThrow)) 7679 { 7680 *didThrow = true; 7681 return -1; 7682 } 7683 if (*didThrow) 7684 return -1; 7685 return 0; 7686 } 7687 if (!nextPage!false()) 7688 { 7689 *didThrow = true; 7690 return -1; 7691 } 7692 } 7693 //return 0; 7694 } 7695 7696 // bisection found our page. seek to it, update pcm offset; easier case than raw_seek, don't keep packets preceding granulepos 7697 bufused = bufpos = 0; 7698 pglength = 0; 7699 curseg = 0; 7700 if (!_io.seek(best, false, _userData)) 7701 { 7702 *didThrow = true; 7703 return -1; 7704 } 7705 if (!nextPage!(false, true)()) 7706 { 7707 *didThrow = true; 7708 return -1; 7709 } 7710 auto rtg = pggranule; 7711 seqno = pgseqno; 7712 // pull out all but last packet; the one right after granulepos 7713 for (int p = 0; p < segments; ++p) 7714 if (seglen[p] < 255) 7715 curseg = p+1; 7716 7717 if (!loadPacket(didThrow)) 7718 { 7719 *didThrow = true; 7720 return -1; 7721 } 7722 if (*didThrow) 7723 return -1; 7724 return rtg; 7725 } 7726 7727 static: 7728 T getMemInt(T) (const(void)* pp) { 7729 static if (is(T == byte) || is(T == ubyte)) { 7730 return *cast(const(ubyte)*)pp; 7731 } else static if (is(T == short) || is(T == ushort)) { 7732 version(LittleEndian) { 7733 return *cast(const(T)*)pp; 7734 } else { 7735 auto pp = cast(const(ubyte)*)pp; 7736 return cast(T)(pp[0]|(pp[1]<<8)); 7737 } 7738 } else static if (is(T == int) || is(T == uint)) { 7739 version(LittleEndian) { 7740 return *cast(const(T)*)pp; 7741 } else { 7742 auto pp = cast(const(ubyte)*)pp; 7743 return cast(T)(pp[0]|(pp[1]<<8)|(pp[2]<<16)|(pp[3]<<24)); 7744 } 7745 } else static if (is(T == long) || is(T == ulong)) { 7746 version(LittleEndian) { 7747 return *cast(const(T)*)pp; 7748 } else { 7749 auto pp = cast(const(ubyte)*)pp; 7750 return cast(T)( 7751 (cast(ulong)pp[0])|((cast(ulong)pp[1])<<8)|((cast(ulong)pp[2])<<16)|((cast(ulong)pp[3])<<24)| 7752 ((cast(ulong)pp[4])<<32)|((cast(ulong)pp[5])<<40)|((cast(ulong)pp[6])<<48)|((cast(ulong)pp[7])<<56) 7753 ); 7754 } 7755 } else { 7756 static assert(0, "invalid type for getMemInt: '"~T.stringof~"'"); 7757 } 7758 } 7759 7760 uint crc32 (const(void)[] buf, uint crc=0) nothrow @trusted @nogc { 7761 static immutable uint[256] crctable = (){ 7762 // helper to initialize lookup for direct-table CRC (illustrative; we use the static init below) 7763 static uint _ogg_crc_entry (uint index) { 7764 uint r = index<<24; 7765 foreach (immutable _; 0..8) { 7766 if (r&0x80000000U) { 7767 r = (r<<1)^0x04c11db7; 7768 /* The same as the ethernet generator 7769 polynomial, although we use an 7770 unreflected alg and an init/final 7771 of 0, not 0xffffffff */ 7772 } else { 7773 r <<= 1; 7774 } 7775 } 7776 return (r&0xffffffffU); 7777 } 7778 uint[256] res; 7779 foreach (immutable idx, ref uint v; res[]) v = _ogg_crc_entry(cast(uint)idx); 7780 return res; 7781 }(); 7782 foreach (ubyte b; cast(const(ubyte)[])buf) crc = (crc<<8)^crctable.ptr[((crc>>24)&0xFF)^b]; 7783 return crc; 7784 } 7785 } 7786 7787 7788 // ////////////////////////////////////////////////////////////////////////// // 7789 nothrow @nogc { 7790 enum OPUS_SEEK_PREROLL_MS = 80; 7791 enum OPUS_HEAD_SIZE = 19; 7792 7793 static int opus_header (AVCtx* avf, ref OggStream ogg) { 7794 //uint8_t *packet = os.buf + os.pstart; 7795 if (ogg.packetBos) { 7796 if (ogg.packetLength < OPUS_HEAD_SIZE || (ogg.packetData[8]&0xF0) != 0) return AVERROR_INVALIDDATA; 7797 //st.codecpar.codec_type = AVMEDIA_TYPE_AUDIO; 7798 //st.codecpar.codec_id = AV_CODEC_ID_OPUS; 7799 //st.codecpar.channels = ost.packetData[8]; 7800 7801 avf.preskip = ogg.getMemInt!ushort(ogg.packetData.ptr+10); 7802 //!!!st.codecpar.initial_padding = priv.pre_skip; 7803 /*orig_sample_rate = AV_RL32(packet + 12);*/ 7804 /*gain = AV_RL16(packet + 16);*/ 7805 /*channel_map = AV_RL8 (packet + 18);*/ 7806 7807 //if (ff_alloc_extradata(st.codecpar, os.psize)) return AVERROR(ENOMEM); 7808 if (avf.extradata) av_free(avf.extradata); 7809 avf.extradata = av_mallocz!ubyte(ogg.packetLength); 7810 if (avf.extradata is null) return -1; 7811 avf.extradata[0..ogg.packetLength] = ogg.packetData[0..ogg.packetLength]; 7812 avf.extradata_size = cast(uint)ogg.packetLength; 7813 7814 //st.codecpar.sample_rate = 48000; 7815 //st.codecpar.seek_preroll = av_rescale(OPUS_SEEK_PREROLL_MS, st.codecpar.sample_rate, 1000); 7816 //avpriv_set_pts_info(st, 64, 1, 48000); 7817 avf.need_comments = 1; 7818 return 2; 7819 } 7820 7821 if (avf.need_comments) { 7822 if (ogg.packetLength < 8 || memcmp(ogg.packetData.ptr, "OpusTags".ptr, 8) != 0) return AVERROR_INVALIDDATA; 7823 //ff_vorbis_stream_comment(avf, st, ogg.packetData.ptr + 8, ogg.packetLength - 8); 7824 --avf.need_comments; 7825 return 1; 7826 } 7827 7828 return 0; 7829 } 7830 7831 static int opus_duration (const(uint8_t)* src, int size) { 7832 uint nb_frames = 1; 7833 uint toc = src[0]; 7834 uint toc_config = toc>>3; 7835 uint toc_count = toc&3; 7836 uint frame_size = toc_config < 12 ? FFMAX(480, 960 * (toc_config & 3)) : 7837 toc_config < 16 ? 480 << (toc_config & 1) : 120 << (toc_config & 3); 7838 if (toc_count == 3) { 7839 if (size < 2) return AVERROR_INVALIDDATA; 7840 nb_frames = src[1]&0x3F; 7841 } else if (toc_count) { 7842 nb_frames = 2; 7843 } 7844 return frame_size*nb_frames; 7845 } 7846 7847 static int opus_packet (AVCtx* avf, ref OggStream ogg) { 7848 int ret; 7849 7850 if (!ogg.packetLength) return AVERROR_INVALIDDATA; 7851 if (ogg.packetGranule > (1UL<<62)) { 7852 //av_log(avf, AV_LOG_ERROR, "Unsupported huge granule pos %"PRId64 "\n", os.granule); 7853 return AVERROR_INVALIDDATA; 7854 } 7855 7856 //if ((!ogg.lastpts || ogg.lastpts == AV_NOPTS_VALUE) && !(ogg.flags & OGG_FLAG_EOS)) 7857 if (ogg.packetGranule != 0 && !ogg.packetEos) { 7858 /*! 7859 int seg, d; 7860 int duration; 7861 uint8_t *last_pkt = os.buf + os.pstart; 7862 uint8_t *next_pkt = last_pkt; 7863 7864 duration = 0; 7865 seg = os.segp; 7866 d = opus_duration(last_pkt, ogg.packetLength); 7867 if (d < 0) { 7868 os.pflags |= AV_PKT_FLAG_CORRUPT; 7869 return 0; 7870 } 7871 duration += d; 7872 last_pkt = next_pkt = next_pkt + ogg.packetLength; 7873 for (; seg < os.nsegs; seg++) { 7874 next_pkt += os.segments[seg]; 7875 if (os.segments[seg] < 255 && next_pkt != last_pkt) { 7876 int d = opus_duration(last_pkt, next_pkt - last_pkt); 7877 if (d > 0) 7878 duration += d; 7879 last_pkt = next_pkt; 7880 } 7881 } 7882 os.lastpts = 7883 os.lastdts = os.granule - duration; 7884 */ 7885 } 7886 7887 if ((ret = opus_duration(ogg.packetData.ptr, ogg.packetLength)) < 0) return ret; 7888 7889 /*! 7890 os.pduration = ret; 7891 if (os.lastpts != AV_NOPTS_VALUE) { 7892 if (st.start_time == AV_NOPTS_VALUE) 7893 st.start_time = os.lastpts; 7894 priv.cur_dts = os.lastdts = os.lastpts -= priv.pre_skip; 7895 } 7896 7897 priv.cur_dts += os.pduration; 7898 if ((os.flags & OGG_FLAG_EOS)) { 7899 int64_t skip = priv.cur_dts - os.granule + priv.pre_skip; 7900 skip = FFMIN(skip, os.pduration); 7901 if (skip > 0) { 7902 os.pduration = skip < os.pduration ? os.pduration - skip : 1; 7903 os.end_trimming = skip; 7904 //av_log(avf, AV_LOG_DEBUG, "Last packet was truncated to %d due to end trimming.\n", os.pduration); 7905 } 7906 } 7907 */ 7908 7909 return 0; 7910 } 7911 7912 } // nothrow @nogc 7913 7914 7915 // ////////////////////////////////////////////////////////////////////////// // 7916 align(1) union TrickyFloatUnion { 7917 align(1): 7918 float f; 7919 int i; 7920 } 7921 static assert(TrickyFloatUnion.i.sizeof == 4 && TrickyFloatUnion.f.sizeof == 4); 7922 // add (1<<23) to convert to int, then divide by 2^SHIFT, then add 0.5/2^SHIFT to round 7923 enum Float2IntScaled(string x, string d) = 7924 "{ TrickyFloatUnion temp = void; temp.f = ("~x~")+(1.5f*(1<<(23-15))+0.5f/(1<<15));"~ 7925 "("~d~") = temp.i-(((150-15)<<23)+(1<<22));"~ 7926 "if (cast(uint)(("~d~")+32768) > 65535) ("~d~") = (("~d~") < 0 ? -32768 : 32767); }"; 7927 7928 7929 // ////////////////////////////////////////////////////////////////////////// // 7930 7931 struct OpusFileCtx { 7932 private: 7933 @nogc: 7934 nothrow: 7935 AVCtx ctx; 7936 ubyte* commbuf; 7937 uint cblen; 7938 OpusContext c; 7939 public OggStream ogg; 7940 OggStream.PageInfo lastpage; 7941 short[960*3*2] samples; 7942 float[960*3*2] sbuffer; 7943 bool wantNewPacket; 7944 ulong curpcm; // for page end; let's hope that nobody will create huge ogg pages 7945 7946 void close () { 7947 av_freep(&commbuf); 7948 av_freep(&ctx.extradata); 7949 opus_decode_close(&c); 7950 ogg.close(); 7951 } 7952 7953 public: 7954 enum rate = 48000; // always 7955 ubyte channels () const pure nothrow @safe @nogc { return cast(ubyte)c.streams[0].output_channels; } 7956 // all timing is in milliseconds 7957 long duration () const pure nothrow @safe @nogc { return (lastpage.granule/48); } 7958 long curtime () const pure nothrow @safe @nogc { return (curpcm/48); } 7959 7960 // in samples, not multiplied by channel count 7961 long smpduration () const pure nothrow @safe @nogc { return lastpage.granule; } 7962 long smpcurtime () const pure nothrow @safe @nogc { return curpcm; } 7963 7964 const(char)[] vendor () const pure nothrow @trusted @nogc { 7965 if (commbuf is null || cblen < 4) return null; 7966 uint len = commbuf[0]|(commbuf[1]<<8)|(commbuf[2]<<16)|(commbuf[3]<<24); 7967 if (len > cblen || cblen-len < 4) return null; 7968 return cast(const(char)[])(commbuf[4..4+len]); 7969 } 7970 7971 uint commentCount () const pure nothrow @trusted @nogc { 7972 if (commbuf is null || cblen < 4) return 0; 7973 uint len = commbuf[0]|(commbuf[1]<<8)|(commbuf[2]<<16)|(commbuf[3]<<24); 7974 if (len > cblen || cblen-len < 4) return 0; 7975 uint cpos = 4+len; 7976 if (cpos >= cblen || cblen-cpos < 4) return 0; 7977 uint count = commbuf[cpos+0]|(commbuf[cpos+1]<<8)|(commbuf[cpos+2]<<16)|(commbuf[cpos+3]<<24); 7978 cpos += 4; 7979 uint res = 0; 7980 while (count > 0 && cpos+4 <= cblen) { 7981 len = commbuf[cpos+0]|(commbuf[cpos+1]<<8)|(commbuf[cpos+2]<<16)|(commbuf[cpos+3]<<24); 7982 cpos += 4; 7983 if (cpos > cblen || cblen-cpos < len) break; 7984 ++res; 7985 cpos += len; 7986 --count; 7987 } 7988 return res; 7989 } 7990 7991 const(char)[] comment (uint cidx) const pure nothrow @trusted @nogc { 7992 if (commbuf is null || cblen < 4) return null; 7993 uint len = commbuf[0]|(commbuf[1]<<8)|(commbuf[2]<<16)|(commbuf[3]<<24); 7994 if (len > cblen || cblen-len < 4) return null; 7995 uint cpos = 4+len; 7996 if (cpos >= cblen || cblen-cpos < 4) return null; 7997 uint count = commbuf[cpos+0]|(commbuf[cpos+1]<<8)|(commbuf[cpos+2]<<16)|(commbuf[cpos+3]<<24); 7998 cpos += 4; 7999 while (count > 0 && cpos+4 <= cblen) { 8000 len = commbuf[cpos+0]|(commbuf[cpos+1]<<8)|(commbuf[cpos+2]<<16)|(commbuf[cpos+3]<<24); 8001 cpos += 4; 8002 if (cpos > cblen || cblen-cpos < len) break; 8003 if (cidx == 0) return cast(const(char)[])(commbuf[cpos..cpos+len]); 8004 --cidx; 8005 cpos += len; 8006 --count; 8007 } 8008 return null; 8009 } 8010 8011 private short getGain () const pure nothrow @trusted @nogc { 8012 if (commbuf is null || cblen < 4) return 0; 8013 uint len = commbuf[0]|(commbuf[1]<<8)|(commbuf[2]<<16)|(commbuf[3]<<24); 8014 if (len > cblen || cblen-len < 4) return 0; 8015 uint cpos = 4+len; 8016 if (cpos >= cblen || cblen-cpos < 4) return 0; 8017 uint count = commbuf[cpos+0]|(commbuf[cpos+1]<<8)|(commbuf[cpos+2]<<16)|(commbuf[cpos+3]<<24); 8018 cpos += 4; 8019 while (count > 0 && cpos+4 <= cblen) { 8020 len = commbuf[cpos+0]|(commbuf[cpos+1]<<8)|(commbuf[cpos+2]<<16)|(commbuf[cpos+3]<<24); 8021 cpos += 4; 8022 if (cpos > cblen || cblen-cpos < len) break; 8023 { 8024 auto cmt = cast(const(char)[])(commbuf[cpos..cpos+len]); 8025 enum GainName = "R128_TRACK_GAIN="; //-573 8026 while (cmt.length && cmt.ptr[0] <= ' ') cmt = cmt[1..$]; 8027 while (cmt.length && cmt[$-1] <= ' ') cmt = cmt[0..$-1]; 8028 if (cmt.length > GainName.length) { 8029 bool ok = true; 8030 foreach (immutable xidx, char ch; cmt[0..GainName.length]) { 8031 if (ch >= 'a' && ch <= 'z') ch -= 32; 8032 if (ch != GainName[xidx]) { ok = false; break; } 8033 } 8034 if (ok) { 8035 bool neg = false; 8036 int v = 0; 8037 cmt = cmt[GainName.length..$]; 8038 if (cmt.length && cmt[0] == '-') { neg = true; cmt = cmt[1..$]; } 8039 else if (cmt.length && cmt[0] == '+') cmt = cmt[1..$]; 8040 if (cmt.length == 0) v = -1; 8041 while (cmt.length) { 8042 int c = cmt.ptr[0]; 8043 cmt = cmt[1..$]; 8044 if (c < '0' || c > '9') { v = -1; break; } 8045 v = v*10+c-'0'; 8046 if ((neg && v > 32768) || (!neg && v > 32767)) { v = -1; break; } 8047 } 8048 if (v >= 0) { 8049 if (neg) v = -v; 8050 return cast(short)v; 8051 } 8052 } 8053 } 8054 } 8055 cpos += len; 8056 --count; 8057 } 8058 return 0; 8059 } 8060 8061 // read and decode one sound frame; return samples or null 8062 short[] readFrame(bool *didThrow) return 8063 { 8064 *didThrow = false; 8065 AVFrame frame; 8066 AVPacket pkt; 8067 ubyte*[2] eptr; 8068 float*[2] fptr; 8069 for (;;) { 8070 if (wantNewPacket) 8071 { 8072 if (!ogg.loadPacket(didThrow)) return null; 8073 if (*didThrow) 8074 return null; 8075 } 8076 //if (ogg.pggranule > 0 && ogg.pggranule != -1 && ogg.pggranule >= ctx.preskip) curpcm = ogg.pggranule-ctx.preskip; 8077 wantNewPacket = true; 8078 frame.linesize[0] = sbuffer.length*sbuffer[0].sizeof; 8079 pkt.data = ogg.packetData.ptr; 8080 pkt.size = cast(uint)ogg.packetLength; 8081 eptr[0] = cast(ubyte*)&sbuffer[0]; 8082 eptr[1] = cast(ubyte*)&sbuffer[sbuffer.length/2]; 8083 fptr[0] = cast(float*)eptr[0]; 8084 fptr[1] = cast(float*)eptr[1]; 8085 frame.extended_data = eptr.ptr; 8086 int gotfrptr = 0; 8087 auto r = opus_decode_packet(&c, &frame, &gotfrptr, &pkt); 8088 if (r < 0) 8089 { 8090 // error processing opus frame 8091 *didThrow = true; 8092 return null; 8093 } 8094 if (!gotfrptr) continue; 8095 curpcm += r; 8096 //if (ogg.packetGranule && ogg.packetGranule != -1) lastgran = ogg.packetGranule-ctx.preskip; 8097 //conwritef!"\r%s:%02s / %s:%02s"((lastgran/48000)/60, (lastgran/48000)%60, (lastpage.granule/48000)/60, (lastpage.granule/48000)%60); 8098 short* dptr = samples.ptr; 8099 int v; 8100 foreach (immutable spos; 0..r) { 8101 foreach (immutable chn; 0..channels) { 8102 mixin(Float2IntScaled!("*fptr[chn]++", "v")); 8103 *dptr++ = cast(short)v; 8104 } 8105 } 8106 return samples.ptr[0..r*channels]; 8107 } 8108 } 8109 } 8110 8111 8112 public alias OpusFile = OpusFileCtx*; 8113 8114 8115 public OpusFile opusOpen (IOCallbacks* io, void* userData, bool* didThrow) nothrow 8116 { 8117 bool err; 8118 8119 *didThrow = false; 8120 OpusFile of = av_mallocz!OpusFileCtx(1); 8121 if (of is null) 8122 goto failure0; 8123 8124 *of = OpusFileCtx.init; // just in case 8125 8126 8127 io.seek(false, false, userData); 8128 8129 of.ogg.setup(io, userData, &err); 8130 if (err) 8131 goto failure1; 8132 8133 if (!of.ogg.findLastPage(of.lastpage)) 8134 goto failure2; 8135 8136 for (;;) { 8137 auto r = opus_header(&of.ctx, of.ogg); 8138 if (r < 0) 8139 goto failure2; 8140 // current packet is tags? 8141 if (of.ogg.packetLength >= 12 && of.commbuf is null && cast(const(char)[])(of.ogg.packetData[0..8]) == "OpusTags") { 8142 of.commbuf = av_mallocz!ubyte(of.ogg.packetLength-8); 8143 if (of.commbuf !is null) { 8144 memcpy(of.commbuf, of.ogg.packetData.ptr+8, of.ogg.packetLength-8); 8145 of.cblen = of.ogg.packetLength-8; 8146 } 8147 } 8148 if (!of.ogg.loadPacket(didThrow)) 8149 goto failure2; 8150 if (*didThrow) 8151 goto failure2; 8152 if (r == 1) break; 8153 } 8154 8155 if (of.ogg.pggranule < of.ctx.preskip) 8156 goto failure2; 8157 if (of.lastpage.granule < of.ctx.preskip) 8158 goto failure2; 8159 of.lastpage.granule -= of.ctx.preskip; 8160 8161 if (opus_decode_init(&of.ctx, &of.c, of.getGain) < 0) 8162 goto failure2; 8163 8164 if (of.c.nb_streams != 1) 8165 goto failure3; 8166 8167 // just in case, check the impossible 8168 if (of.c.streams[0].output_channels < 1 || of.c.streams[0].output_channels > 2) 8169 goto failure3; // only mono and stereo opus streams are supported" 8170 8171 return of; 8172 8173 failure3: 8174 opus_decode_close(&of.c); 8175 8176 failure2: 8177 of.ogg.close(); 8178 8179 failure1: 8180 av_freep(&of.commbuf); 8181 av_freep(&of.ctx.extradata); 8182 av_free(of); 8183 8184 failure0: 8185 *didThrow = true; 8186 return null; 8187 } 8188 8189 8190 public void opusClose (ref OpusFile of) { 8191 if (of !is null) { 8192 of.close(); 8193 av_freep(&of); 8194 } 8195 }