@scrypted/libav 1.0.167 → 1.0.168

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (720) hide show
  1. package/opus/.gitattributes +10 -0
  2. package/opus/.gitlab-ci.yml +122 -0
  3. package/opus/AUTHORS +6 -0
  4. package/opus/CMakeLists.txt +755 -0
  5. package/opus/COPYING +44 -0
  6. package/opus/ChangeLog +0 -0
  7. package/opus/LICENSE_PLEASE_READ.txt +22 -0
  8. package/opus/Makefile.am +497 -0
  9. package/opus/Makefile.mips +169 -0
  10. package/opus/Makefile.unix +167 -0
  11. package/opus/NEWS +0 -0
  12. package/opus/README +189 -0
  13. package/opus/README.draft +54 -0
  14. package/opus/autogen.bat +13 -0
  15. package/opus/autogen.sh +16 -0
  16. package/opus/celt/_kiss_fft_guts.h +188 -0
  17. package/opus/celt/arch.h +390 -0
  18. package/opus/celt/arm/arm2gnu.pl +353 -0
  19. package/opus/celt/arm/arm_celt_map.c +171 -0
  20. package/opus/celt/arm/armcpu.c +291 -0
  21. package/opus/celt/arm/armcpu.h +90 -0
  22. package/opus/celt/arm/armopts.s.in +37 -0
  23. package/opus/celt/arm/celt_fft_ne10.c +173 -0
  24. package/opus/celt/arm/celt_mdct_ne10.c +258 -0
  25. package/opus/celt/arm/celt_neon_intr.c +272 -0
  26. package/opus/celt/arm/celt_pitch_xcorr_arm.s +551 -0
  27. package/opus/celt/arm/fft_arm.h +71 -0
  28. package/opus/celt/arm/fixed_arm64.h +35 -0
  29. package/opus/celt/arm/fixed_armv4.h +80 -0
  30. package/opus/celt/arm/fixed_armv5e.h +151 -0
  31. package/opus/celt/arm/kiss_fft_armv4.h +121 -0
  32. package/opus/celt/arm/kiss_fft_armv5e.h +118 -0
  33. package/opus/celt/arm/mdct_arm.h +59 -0
  34. package/opus/celt/arm/meson.build +12 -0
  35. package/opus/celt/arm/pitch_arm.h +160 -0
  36. package/opus/celt/arm/pitch_neon_intr.c +288 -0
  37. package/opus/celt/bands.c +1686 -0
  38. package/opus/celt/bands.h +123 -0
  39. package/opus/celt/celt.c +317 -0
  40. package/opus/celt/celt.h +255 -0
  41. package/opus/celt/celt_decoder.c +1616 -0
  42. package/opus/celt/celt_encoder.c +2854 -0
  43. package/opus/celt/celt_lpc.c +364 -0
  44. package/opus/celt/celt_lpc.h +66 -0
  45. package/opus/celt/cpu_support.h +72 -0
  46. package/opus/celt/cwrs.c +715 -0
  47. package/opus/celt/cwrs.h +48 -0
  48. package/opus/celt/dump_modes/dump_modes.c +400 -0
  49. package/opus/celt/dump_modes/dump_modes_arch.h +45 -0
  50. package/opus/celt/dump_modes/dump_modes_arm_ne10.c +152 -0
  51. package/opus/celt/ecintrin.h +91 -0
  52. package/opus/celt/entcode.c +153 -0
  53. package/opus/celt/entcode.h +152 -0
  54. package/opus/celt/entdec.c +266 -0
  55. package/opus/celt/entdec.h +110 -0
  56. package/opus/celt/entenc.c +305 -0
  57. package/opus/celt/entenc.h +119 -0
  58. package/opus/celt/fixed_c5x.h +79 -0
  59. package/opus/celt/fixed_c6x.h +70 -0
  60. package/opus/celt/fixed_debug.h +839 -0
  61. package/opus/celt/fixed_generic.h +201 -0
  62. package/opus/celt/float_cast.h +152 -0
  63. package/opus/celt/kiss_fft.c +648 -0
  64. package/opus/celt/kiss_fft.h +217 -0
  65. package/opus/celt/laplace.c +235 -0
  66. package/opus/celt/laplace.h +57 -0
  67. package/opus/celt/mathops.c +217 -0
  68. package/opus/celt/mathops.h +479 -0
  69. package/opus/celt/mdct.c +372 -0
  70. package/opus/celt/mdct.h +115 -0
  71. package/opus/celt/meson.build +64 -0
  72. package/opus/celt/mfrngcod.h +48 -0
  73. package/opus/celt/mips/celt_mipsr1.h +152 -0
  74. package/opus/celt/mips/fixed_generic_mipsr1.h +126 -0
  75. package/opus/celt/mips/kiss_fft_mipsr1.h +167 -0
  76. package/opus/celt/mips/mdct_mipsr1.h +288 -0
  77. package/opus/celt/mips/pitch_mipsr1.h +161 -0
  78. package/opus/celt/mips/vq_mipsr1.h +116 -0
  79. package/opus/celt/modes.c +450 -0
  80. package/opus/celt/modes.h +75 -0
  81. package/opus/celt/opus_custom_demo.c +451 -0
  82. package/opus/celt/os_support.h +99 -0
  83. package/opus/celt/pitch.c +555 -0
  84. package/opus/celt/pitch.h +203 -0
  85. package/opus/celt/quant_bands.c +562 -0
  86. package/opus/celt/quant_bands.h +66 -0
  87. package/opus/celt/rate.c +646 -0
  88. package/opus/celt/rate.h +101 -0
  89. package/opus/celt/stack_alloc.h +185 -0
  90. package/opus/celt/static_modes_fixed.h +1541 -0
  91. package/opus/celt/static_modes_fixed_arm_ne10.h +388 -0
  92. package/opus/celt/static_modes_float.h +888 -0
  93. package/opus/celt/static_modes_float_arm_ne10.h +404 -0
  94. package/opus/celt/tests/meson.build +19 -0
  95. package/opus/celt/tests/test_unit_cwrs32.c +162 -0
  96. package/opus/celt/tests/test_unit_dft.c +181 -0
  97. package/opus/celt/tests/test_unit_entropy.c +383 -0
  98. package/opus/celt/tests/test_unit_laplace.c +94 -0
  99. package/opus/celt/tests/test_unit_mathops.c +369 -0
  100. package/opus/celt/tests/test_unit_mdct.c +233 -0
  101. package/opus/celt/tests/test_unit_rotation.c +87 -0
  102. package/opus/celt/tests/test_unit_types.c +50 -0
  103. package/opus/celt/vq.c +442 -0
  104. package/opus/celt/vq.h +79 -0
  105. package/opus/celt/x86/celt_lpc_sse.h +67 -0
  106. package/opus/celt/x86/celt_lpc_sse4_1.c +96 -0
  107. package/opus/celt/x86/pitch_avx.c +101 -0
  108. package/opus/celt/x86/pitch_sse.c +185 -0
  109. package/opus/celt/x86/pitch_sse.h +216 -0
  110. package/opus/celt/x86/pitch_sse2.c +95 -0
  111. package/opus/celt/x86/pitch_sse4_1.c +240 -0
  112. package/opus/celt/x86/vq_sse.h +52 -0
  113. package/opus/celt/x86/vq_sse2.c +217 -0
  114. package/opus/celt/x86/x86_arch_macros.h +47 -0
  115. package/opus/celt/x86/x86_celt_map.c +187 -0
  116. package/opus/celt/x86/x86cpu.c +184 -0
  117. package/opus/celt/x86/x86cpu.h +112 -0
  118. package/opus/celt_headers.mk +54 -0
  119. package/opus/celt_sources.mk +55 -0
  120. package/opus/cmake/CFeatureCheck.cmake +39 -0
  121. package/opus/cmake/OpusBuildtype.cmake +27 -0
  122. package/opus/cmake/OpusConfig.cmake +118 -0
  123. package/opus/cmake/OpusConfig.cmake.in +20 -0
  124. package/opus/cmake/OpusFunctions.cmake +231 -0
  125. package/opus/cmake/OpusPackageVersion.cmake +70 -0
  126. package/opus/cmake/OpusSources.cmake +71 -0
  127. package/opus/cmake/README.md +132 -0
  128. package/opus/cmake/RunTest.cmake +61 -0
  129. package/opus/cmake/config.h.cmake.in +1 -0
  130. package/opus/cmake/cpu_info_by_asm.c +31 -0
  131. package/opus/cmake/cpu_info_by_c.c +9 -0
  132. package/opus/cmake/vla.c +7 -0
  133. package/opus/configure.ac +1091 -0
  134. package/opus/dnn/LPCNet.yml +24 -0
  135. package/opus/dnn/README +1 -0
  136. package/opus/dnn/README.md +126 -0
  137. package/opus/dnn/adaconvtest.c +449 -0
  138. package/opus/dnn/arm/arm_dnn_map.c +88 -0
  139. package/opus/dnn/arm/dnn_arm.h +104 -0
  140. package/opus/dnn/arm/nnet_dotprod.c +38 -0
  141. package/opus/dnn/arm/nnet_neon.c +38 -0
  142. package/opus/dnn/burg.c +246 -0
  143. package/opus/dnn/burg.h +41 -0
  144. package/opus/dnn/common.h +56 -0
  145. package/opus/dnn/datasets.txt +163 -0
  146. package/opus/dnn/download_model.bat +9 -0
  147. package/opus/dnn/download_model.sh +30 -0
  148. package/opus/dnn/dred_coding.c +44 -0
  149. package/opus/dnn/dred_coding.h +36 -0
  150. package/opus/dnn/dred_config.h +54 -0
  151. package/opus/dnn/dred_decoder.c +129 -0
  152. package/opus/dnn/dred_decoder.h +49 -0
  153. package/opus/dnn/dred_encoder.c +363 -0
  154. package/opus/dnn/dred_encoder.h +71 -0
  155. package/opus/dnn/dred_rdovae.h +42 -0
  156. package/opus/dnn/dred_rdovae_dec.c +139 -0
  157. package/opus/dnn/dred_rdovae_dec.h +53 -0
  158. package/opus/dnn/dred_rdovae_enc.c +110 -0
  159. package/opus/dnn/dred_rdovae_enc.h +52 -0
  160. package/opus/dnn/dump_data.c +238 -0
  161. package/opus/dnn/dump_lpcnet_tables.c +104 -0
  162. package/opus/dnn/fargan.c +225 -0
  163. package/opus/dnn/fargan.h +68 -0
  164. package/opus/dnn/fargan_demo.c +217 -0
  165. package/opus/dnn/freq.c +328 -0
  166. package/opus/dnn/freq.h +61 -0
  167. package/opus/dnn/fwgan.c +322 -0
  168. package/opus/dnn/fwgan.h +83 -0
  169. package/opus/dnn/kiss99.c +81 -0
  170. package/opus/dnn/kiss99.h +46 -0
  171. package/opus/dnn/lossgen.c +192 -0
  172. package/opus/dnn/lossgen.h +55 -0
  173. package/opus/dnn/lossgen_demo.c +22 -0
  174. package/opus/dnn/lpcnet.c +283 -0
  175. package/opus/dnn/lpcnet.h +183 -0
  176. package/opus/dnn/lpcnet_enc.c +230 -0
  177. package/opus/dnn/lpcnet_plc.c +211 -0
  178. package/opus/dnn/lpcnet_private.h +90 -0
  179. package/opus/dnn/lpcnet_tables.c +307 -0
  180. package/opus/dnn/meson.build +64 -0
  181. package/opus/dnn/nndsp.c +416 -0
  182. package/opus/dnn/nndsp.h +143 -0
  183. package/opus/dnn/nnet.c +149 -0
  184. package/opus/dnn/nnet.h +163 -0
  185. package/opus/dnn/nnet_arch.h +247 -0
  186. package/opus/dnn/nnet_default.c +35 -0
  187. package/opus/dnn/osce.c +1419 -0
  188. package/opus/dnn/osce.h +84 -0
  189. package/opus/dnn/osce_config.h +60 -0
  190. package/opus/dnn/osce_features.c +454 -0
  191. package/opus/dnn/osce_features.h +50 -0
  192. package/opus/dnn/osce_structs.h +125 -0
  193. package/opus/dnn/parse_lpcnet_weights.c +238 -0
  194. package/opus/dnn/pitchdnn.c +79 -0
  195. package/opus/dnn/pitchdnn.h +34 -0
  196. package/opus/dnn/tansig_table.h +50 -0
  197. package/opus/dnn/test_vec.c +128 -0
  198. package/opus/dnn/torch/dnntools/dnntools/__init__.py +2 -0
  199. package/opus/dnn/torch/dnntools/dnntools/quantization/__init__.py +1 -0
  200. package/opus/dnn/torch/dnntools/dnntools/quantization/softquant.py +113 -0
  201. package/opus/dnn/torch/dnntools/dnntools/relegance/__init__.py +2 -0
  202. package/opus/dnn/torch/dnntools/dnntools/relegance/meta_critic.py +85 -0
  203. package/opus/dnn/torch/dnntools/dnntools/relegance/relegance.py +449 -0
  204. package/opus/dnn/torch/dnntools/dnntools/sparsification/__init__.py +6 -0
  205. package/opus/dnn/torch/dnntools/dnntools/sparsification/base_sparsifier.py +58 -0
  206. package/opus/dnn/torch/dnntools/dnntools/sparsification/common.py +123 -0
  207. package/opus/dnn/torch/dnntools/dnntools/sparsification/conv1d_sparsifier.py +133 -0
  208. package/opus/dnn/torch/dnntools/dnntools/sparsification/conv_transpose1d_sparsifier.py +134 -0
  209. package/opus/dnn/torch/dnntools/dnntools/sparsification/gru_sparsifier.py +178 -0
  210. package/opus/dnn/torch/dnntools/dnntools/sparsification/linear_sparsifier.py +128 -0
  211. package/opus/dnn/torch/dnntools/dnntools/sparsification/utils.py +64 -0
  212. package/opus/dnn/torch/dnntools/requirements.txt +1 -0
  213. package/opus/dnn/torch/dnntools/setup.py +48 -0
  214. package/opus/dnn/torch/fargan/README.md +54 -0
  215. package/opus/dnn/torch/fargan/adv_train_fargan.py +277 -0
  216. package/opus/dnn/torch/fargan/dataset.py +61 -0
  217. package/opus/dnn/torch/fargan/dump_fargan_weights.py +112 -0
  218. package/opus/dnn/torch/fargan/fargan.py +322 -0
  219. package/opus/dnn/torch/fargan/filters.py +46 -0
  220. package/opus/dnn/torch/fargan/rc.py +29 -0
  221. package/opus/dnn/torch/fargan/stft_loss.py +186 -0
  222. package/opus/dnn/torch/fargan/test_fargan.py +128 -0
  223. package/opus/dnn/torch/fargan/train_fargan.py +168 -0
  224. package/opus/dnn/torch/fwgan/dump_model_weights.py +88 -0
  225. package/opus/dnn/torch/fwgan/inference.py +141 -0
  226. package/opus/dnn/torch/fwgan/models/__init__.py +7 -0
  227. package/opus/dnn/torch/fwgan/models/fwgan400.py +308 -0
  228. package/opus/dnn/torch/fwgan/models/fwgan500.py +260 -0
  229. package/opus/dnn/torch/lossgen/README.md +27 -0
  230. package/opus/dnn/torch/lossgen/export_lossgen.py +101 -0
  231. package/opus/dnn/torch/lossgen/lossgen.py +29 -0
  232. package/opus/dnn/torch/lossgen/process_data.sh +17 -0
  233. package/opus/dnn/torch/lossgen/test_lossgen.py +42 -0
  234. package/opus/dnn/torch/lossgen/train_lossgen.py +99 -0
  235. package/opus/dnn/torch/lpcnet/README.md +27 -0
  236. package/opus/dnn/torch/lpcnet/add_dataset_config.py +77 -0
  237. package/opus/dnn/torch/lpcnet/data/__init__.py +1 -0
  238. package/opus/dnn/torch/lpcnet/data/lpcnet_dataset.py +227 -0
  239. package/opus/dnn/torch/lpcnet/engine/lpcnet_engine.py +141 -0
  240. package/opus/dnn/torch/lpcnet/make_default_setup.py +56 -0
  241. package/opus/dnn/torch/lpcnet/make_test_config.py +78 -0
  242. package/opus/dnn/torch/lpcnet/models/__init__.py +8 -0
  243. package/opus/dnn/torch/lpcnet/models/lpcnet.py +303 -0
  244. package/opus/dnn/torch/lpcnet/models/multi_rate_lpcnet.py +437 -0
  245. package/opus/dnn/torch/lpcnet/print_lpcnet_complexity.py +64 -0
  246. package/opus/dnn/torch/lpcnet/scripts/collect_multi_run_results.py +190 -0
  247. package/opus/dnn/torch/lpcnet/scripts/loop_run.sh +52 -0
  248. package/opus/dnn/torch/lpcnet/scripts/make_animation.py +67 -0
  249. package/opus/dnn/torch/lpcnet/scripts/modify_dataset_target.py +17 -0
  250. package/opus/dnn/torch/lpcnet/scripts/multi_run.sh +17 -0
  251. package/opus/dnn/torch/lpcnet/scripts/run_inference_test.sh +22 -0
  252. package/opus/dnn/torch/lpcnet/scripts/update_checkpoints.py +54 -0
  253. package/opus/dnn/torch/lpcnet/scripts/update_output_folder.sh +22 -0
  254. package/opus/dnn/torch/lpcnet/scripts/update_setups.py +57 -0
  255. package/opus/dnn/torch/lpcnet/test_lpcnet.py +89 -0
  256. package/opus/dnn/torch/lpcnet/train_lpcnet.py +272 -0
  257. package/opus/dnn/torch/lpcnet/utils/__init__.py +4 -0
  258. package/opus/dnn/torch/lpcnet/utils/data.py +141 -0
  259. package/opus/dnn/torch/lpcnet/utils/endoscopy.py +234 -0
  260. package/opus/dnn/torch/lpcnet/utils/layers/__init__.py +3 -0
  261. package/opus/dnn/torch/lpcnet/utils/layers/dual_fc.py +44 -0
  262. package/opus/dnn/torch/lpcnet/utils/layers/pcm_embeddings.py +71 -0
  263. package/opus/dnn/torch/lpcnet/utils/layers/subconditioner.py +497 -0
  264. package/opus/dnn/torch/lpcnet/utils/misc.py +65 -0
  265. package/opus/dnn/torch/lpcnet/utils/pcm.py +35 -0
  266. package/opus/dnn/torch/lpcnet/utils/sample.py +44 -0
  267. package/opus/dnn/torch/lpcnet/utils/sparsification/__init__.py +2 -0
  268. package/opus/dnn/torch/lpcnet/utils/sparsification/common.py +121 -0
  269. package/opus/dnn/torch/lpcnet/utils/sparsification/gru_sparsifier.py +187 -0
  270. package/opus/dnn/torch/lpcnet/utils/templates.py +157 -0
  271. package/opus/dnn/torch/lpcnet/utils/ulaw.py +58 -0
  272. package/opus/dnn/torch/lpcnet/utils/wav.py +43 -0
  273. package/opus/dnn/torch/neural-pitch/README.md +18 -0
  274. package/opus/dnn/torch/neural-pitch/data_augmentation.py +149 -0
  275. package/opus/dnn/torch/neural-pitch/download_demand.sh +43 -0
  276. package/opus/dnn/torch/neural-pitch/evaluation.py +349 -0
  277. package/opus/dnn/torch/neural-pitch/experiments.py +38 -0
  278. package/opus/dnn/torch/neural-pitch/export_neuralpitch_weights.py +109 -0
  279. package/opus/dnn/torch/neural-pitch/models.py +178 -0
  280. package/opus/dnn/torch/neural-pitch/neural_pitch_update.py +179 -0
  281. package/opus/dnn/torch/neural-pitch/ptdb_process.sh +34 -0
  282. package/opus/dnn/torch/neural-pitch/run_crepe.py +72 -0
  283. package/opus/dnn/torch/neural-pitch/training.py +162 -0
  284. package/opus/dnn/torch/neural-pitch/utils.py +59 -0
  285. package/opus/dnn/torch/osce/README.md +66 -0
  286. package/opus/dnn/torch/osce/adv_train_model.py +462 -0
  287. package/opus/dnn/torch/osce/adv_train_vocoder.py +451 -0
  288. package/opus/dnn/torch/osce/create_testvectors.py +165 -0
  289. package/opus/dnn/torch/osce/data/__init__.py +2 -0
  290. package/opus/dnn/torch/osce/data/lpcnet_vocoding_dataset.py +225 -0
  291. package/opus/dnn/torch/osce/data/silk_conversion_set.py +132 -0
  292. package/opus/dnn/torch/osce/data/silk_enhancement_set.py +140 -0
  293. package/opus/dnn/torch/osce/engine/engine.py +103 -0
  294. package/opus/dnn/torch/osce/engine/vocoder_engine.py +101 -0
  295. package/opus/dnn/torch/osce/export_model_weights.py +174 -0
  296. package/opus/dnn/torch/osce/losses/stft_loss.py +277 -0
  297. package/opus/dnn/torch/osce/losses/td_lowpass.py +34 -0
  298. package/opus/dnn/torch/osce/make_default_setup.py +93 -0
  299. package/opus/dnn/torch/osce/models/__init__.py +42 -0
  300. package/opus/dnn/torch/osce/models/fd_discriminator.py +974 -0
  301. package/opus/dnn/torch/osce/models/lace.py +190 -0
  302. package/opus/dnn/torch/osce/models/lavoce.py +274 -0
  303. package/opus/dnn/torch/osce/models/lavoce_400.py +254 -0
  304. package/opus/dnn/torch/osce/models/lpcnet_feature_net.py +91 -0
  305. package/opus/dnn/torch/osce/models/nns_base.py +69 -0
  306. package/opus/dnn/torch/osce/models/no_lace.py +218 -0
  307. package/opus/dnn/torch/osce/models/scale_embedding.py +68 -0
  308. package/opus/dnn/torch/osce/models/shape_up_48.py +179 -0
  309. package/opus/dnn/torch/osce/models/silk_feature_net.py +86 -0
  310. package/opus/dnn/torch/osce/models/silk_feature_net_pl.py +127 -0
  311. package/opus/dnn/torch/osce/requirements.txt +9 -0
  312. package/opus/dnn/torch/osce/resources/training_files.txt +107716 -0
  313. package/opus/dnn/torch/osce/resources/validation_files.txt +26929 -0
  314. package/opus/dnn/torch/osce/scripts/concatenator.py +85 -0
  315. package/opus/dnn/torch/osce/silk_16_to_48.py +28 -0
  316. package/opus/dnn/torch/osce/stndrd/evaluation/commonvoice_clip_selection.py +123 -0
  317. package/opus/dnn/torch/osce/stndrd/evaluation/create_input_data.sh +25 -0
  318. package/opus/dnn/torch/osce/stndrd/evaluation/env.rc +7 -0
  319. package/opus/dnn/torch/osce/stndrd/evaluation/evaluate.py +113 -0
  320. package/opus/dnn/torch/osce/stndrd/evaluation/lace_loss_metric.py +330 -0
  321. package/opus/dnn/torch/osce/stndrd/evaluation/make_boxplots.py +116 -0
  322. package/opus/dnn/torch/osce/stndrd/evaluation/make_boxplots_moctest.py +109 -0
  323. package/opus/dnn/torch/osce/stndrd/evaluation/make_tables.py +124 -0
  324. package/opus/dnn/torch/osce/stndrd/evaluation/make_tables_moctest.py +121 -0
  325. package/opus/dnn/torch/osce/stndrd/evaluation/moc.py +182 -0
  326. package/opus/dnn/torch/osce/stndrd/evaluation/moc2.py +190 -0
  327. package/opus/dnn/torch/osce/stndrd/evaluation/process_dataset.sh +98 -0
  328. package/opus/dnn/torch/osce/stndrd/evaluation/run_nomad.py +138 -0
  329. package/opus/dnn/torch/osce/stndrd/evaluation/run_osce_test.py +196 -0
  330. package/opus/dnn/torch/osce/stndrd/presentation/endoscopy.py +205 -0
  331. package/opus/dnn/torch/osce/stndrd/presentation/lace_demo.ipynb +313 -0
  332. package/opus/dnn/torch/osce/stndrd/presentation/linear_prediction.ipynb +320 -0
  333. package/opus/dnn/torch/osce/stndrd/presentation/playback.py +25 -0
  334. package/opus/dnn/torch/osce/stndrd/presentation/postfilter.ipynb +275 -0
  335. package/opus/dnn/torch/osce/stndrd/presentation/spectrogram.ipynb +173 -0
  336. package/opus/dnn/torch/osce/test_model.py +96 -0
  337. package/opus/dnn/torch/osce/test_vocoder.py +103 -0
  338. package/opus/dnn/torch/osce/train_model.py +307 -0
  339. package/opus/dnn/torch/osce/train_vocoder.py +287 -0
  340. package/opus/dnn/torch/osce/utils/ada_conv.py +71 -0
  341. package/opus/dnn/torch/osce/utils/complexity.py +8 -0
  342. package/opus/dnn/torch/osce/utils/endoscopy.py +205 -0
  343. package/opus/dnn/torch/osce/utils/layers/fir.py +27 -0
  344. package/opus/dnn/torch/osce/utils/layers/limited_adaptive_comb1d.py +230 -0
  345. package/opus/dnn/torch/osce/utils/layers/limited_adaptive_conv1d.py +200 -0
  346. package/opus/dnn/torch/osce/utils/layers/noise_shaper.py +100 -0
  347. package/opus/dnn/torch/osce/utils/layers/pitch_auto_correlator.py +84 -0
  348. package/opus/dnn/torch/osce/utils/layers/silk_upsampler.py +167 -0
  349. package/opus/dnn/torch/osce/utils/layers/td_shaper.py +145 -0
  350. package/opus/dnn/torch/osce/utils/lpcnet_features.py +112 -0
  351. package/opus/dnn/torch/osce/utils/misc.py +95 -0
  352. package/opus/dnn/torch/osce/utils/moc.py +153 -0
  353. package/opus/dnn/torch/osce/utils/pitch.py +122 -0
  354. package/opus/dnn/torch/osce/utils/silk_features.py +144 -0
  355. package/opus/dnn/torch/osce/utils/softquant.py +110 -0
  356. package/opus/dnn/torch/osce/utils/spec.py +210 -0
  357. package/opus/dnn/torch/osce/utils/templates.py +347 -0
  358. package/opus/dnn/torch/plc/export_plc.py +100 -0
  359. package/opus/dnn/torch/plc/plc.py +144 -0
  360. package/opus/dnn/torch/plc/plc_dataset.py +60 -0
  361. package/opus/dnn/torch/plc/train_plc.py +145 -0
  362. package/opus/dnn/torch/rdovae/README.md +55 -0
  363. package/opus/dnn/torch/rdovae/download_datasets.sh +6 -0
  364. package/opus/dnn/torch/rdovae/export_rdovae_weights.py +365 -0
  365. package/opus/dnn/torch/rdovae/fec_encoder.py +212 -0
  366. package/opus/dnn/torch/rdovae/import_rdovae_weights.py +143 -0
  367. package/opus/dnn/torch/rdovae/packets/__init__.py +1 -0
  368. package/opus/dnn/torch/rdovae/packets/fec_packets.c +142 -0
  369. package/opus/dnn/torch/rdovae/packets/fec_packets.h +34 -0
  370. package/opus/dnn/torch/rdovae/packets/fec_packets.py +108 -0
  371. package/opus/dnn/torch/rdovae/process_speech.sh +7 -0
  372. package/opus/dnn/torch/rdovae/rdovae/__init__.py +2 -0
  373. package/opus/dnn/torch/rdovae/rdovae/dataset.py +67 -0
  374. package/opus/dnn/torch/rdovae/rdovae/rdovae.py +719 -0
  375. package/opus/dnn/torch/rdovae/requirements.txt +4 -0
  376. package/opus/dnn/torch/rdovae/train_rdovae.py +278 -0
  377. package/opus/dnn/torch/testsuite/README.md +46 -0
  378. package/opus/dnn/torch/testsuite/examples/lpcnet_c_example.yml +6 -0
  379. package/opus/dnn/torch/testsuite/examples/lpcnet_c_plc_example.yml +5 -0
  380. package/opus/dnn/torch/testsuite/examples/lpcnet_torch_example.yml +5 -0
  381. package/opus/dnn/torch/testsuite/requirements.txt +12 -0
  382. package/opus/dnn/torch/testsuite/run_test.py +375 -0
  383. package/opus/dnn/torch/testsuite/utils/__init__.py +0 -0
  384. package/opus/dnn/torch/testsuite/utils/files.py +54 -0
  385. package/opus/dnn/torch/testsuite/utils/pesq.py +43 -0
  386. package/opus/dnn/torch/testsuite/utils/pitch.py +61 -0
  387. package/opus/dnn/torch/weight-exchange/README.md +21 -0
  388. package/opus/dnn/torch/weight-exchange/requirements.txt +1 -0
  389. package/opus/dnn/torch/weight-exchange/setup.py +48 -0
  390. package/opus/dnn/torch/weight-exchange/wexchange/__init__.py +30 -0
  391. package/opus/dnn/torch/weight-exchange/wexchange/c_export/__init__.py +31 -0
  392. package/opus/dnn/torch/weight-exchange/wexchange/c_export/c_writer.py +181 -0
  393. package/opus/dnn/torch/weight-exchange/wexchange/c_export/common.py +386 -0
  394. package/opus/dnn/torch/weight-exchange/wexchange/tf/__init__.py +5 -0
  395. package/opus/dnn/torch/weight-exchange/wexchange/tf/tf.py +188 -0
  396. package/opus/dnn/torch/weight-exchange/wexchange/torch/__init__.py +37 -0
  397. package/opus/dnn/torch/weight-exchange/wexchange/torch/torch.py +433 -0
  398. package/opus/dnn/training_tf2/dataloader.py +49 -0
  399. package/opus/dnn/training_tf2/decode_rdovae.py +111 -0
  400. package/opus/dnn/training_tf2/diffembed.py +49 -0
  401. package/opus/dnn/training_tf2/dump_lpcnet.py +388 -0
  402. package/opus/dnn/training_tf2/dump_plc.py +296 -0
  403. package/opus/dnn/training_tf2/dump_rdovae.py +306 -0
  404. package/opus/dnn/training_tf2/encode_rdovae.py +125 -0
  405. package/opus/dnn/training_tf2/fec_encoder.py +256 -0
  406. package/opus/dnn/training_tf2/fec_packets.c +142 -0
  407. package/opus/dnn/training_tf2/fec_packets.h +34 -0
  408. package/opus/dnn/training_tf2/fec_packets.py +108 -0
  409. package/opus/dnn/training_tf2/keraslayerdump.py +189 -0
  410. package/opus/dnn/training_tf2/lossfuncs.py +99 -0
  411. package/opus/dnn/training_tf2/lpcnet.py +339 -0
  412. package/opus/dnn/training_tf2/lpcnet_plc.py +101 -0
  413. package/opus/dnn/training_tf2/mdense.py +95 -0
  414. package/opus/dnn/training_tf2/pade.py +70 -0
  415. package/opus/dnn/training_tf2/parameters.py +29 -0
  416. package/opus/dnn/training_tf2/plc_loader.py +73 -0
  417. package/opus/dnn/training_tf2/rdovae.py +372 -0
  418. package/opus/dnn/training_tf2/rdovae_exchange.py +138 -0
  419. package/opus/dnn/training_tf2/rdovae_import.py +123 -0
  420. package/opus/dnn/training_tf2/test_lpcnet.py +120 -0
  421. package/opus/dnn/training_tf2/test_plc.py +92 -0
  422. package/opus/dnn/training_tf2/tf_funcs.py +70 -0
  423. package/opus/dnn/training_tf2/train_lpcnet.py +214 -0
  424. package/opus/dnn/training_tf2/train_plc.py +197 -0
  425. package/opus/dnn/training_tf2/train_rdovae.py +151 -0
  426. package/opus/dnn/training_tf2/ulaw.py +19 -0
  427. package/opus/dnn/training_tf2/uniform_noise.py +78 -0
  428. package/opus/dnn/vec.h +389 -0
  429. package/opus/dnn/vec_avx.h +884 -0
  430. package/opus/dnn/vec_neon.h +473 -0
  431. package/opus/dnn/write_lpcnet_weights.c +97 -0
  432. package/opus/dnn/x86/dnn_x86.h +121 -0
  433. package/opus/dnn/x86/nnet_avx2.c +40 -0
  434. package/opus/dnn/x86/nnet_sse2.c +40 -0
  435. package/opus/dnn/x86/nnet_sse4_1.c +40 -0
  436. package/opus/dnn/x86/x86_dnn_map.c +83 -0
  437. package/opus/doc/Doxyfile.in +340 -0
  438. package/opus/doc/Makefile.am +45 -0
  439. package/opus/doc/build_draft.sh +113 -0
  440. package/opus/doc/build_isobmff.sh +50 -0
  441. package/opus/doc/build_oggdraft.sh +52 -0
  442. package/opus/doc/customdoxygen.css +1011 -0
  443. package/opus/doc/draft-ietf-codec-oggopus.xml +1873 -0
  444. package/opus/doc/draft-ietf-codec-opus-update.xml +513 -0
  445. package/opus/doc/draft-ietf-codec-opus.xml +8276 -0
  446. package/opus/doc/draft-ietf-payload-rtp-opus.xml +960 -0
  447. package/opus/doc/footer.html +32 -0
  448. package/opus/doc/header.html +61 -0
  449. package/opus/doc/meson.build +33 -0
  450. package/opus/doc/opus_in_isobmff.css +60 -0
  451. package/opus/doc/opus_in_isobmff.html +692 -0
  452. package/opus/doc/opus_logo.svg +157 -0
  453. package/opus/doc/opus_update.patch +244 -0
  454. package/opus/doc/release.txt +43 -0
  455. package/opus/doc/trivial_example.c +171 -0
  456. package/opus/include/meson.build +13 -0
  457. package/opus/include/opus.h +1172 -0
  458. package/opus/include/opus_custom.h +381 -0
  459. package/opus/include/opus_defines.h +830 -0
  460. package/opus/include/opus_multistream.h +736 -0
  461. package/opus/include/opus_projection.h +643 -0
  462. package/opus/include/opus_types.h +166 -0
  463. package/opus/lpcnet_headers.mk +44 -0
  464. package/opus/lpcnet_sources.mk +44 -0
  465. package/opus/m4/as-gcc-inline-assembly.m4 +98 -0
  466. package/opus/m4/ax_add_fortify_source.m4 +53 -0
  467. package/opus/m4/opus-intrinsics.m4 +29 -0
  468. package/opus/meson/README.md +88 -0
  469. package/opus/meson/get-version.py +86 -0
  470. package/opus/meson/read-sources-list.py +28 -0
  471. package/opus/meson.build +745 -0
  472. package/opus/meson_options.txt +27 -0
  473. package/opus/opus-uninstalled.pc.in +12 -0
  474. package/opus/opus.m4 +117 -0
  475. package/opus/opus.pc.in +16 -0
  476. package/opus/opus_headers.mk +8 -0
  477. package/opus/opus_sources.mk +17 -0
  478. package/opus/releases.sha2 +85 -0
  479. package/opus/scripts/dump_rnn.py +57 -0
  480. package/opus/scripts/local_build.py +220 -0
  481. package/opus/scripts/rnn_train.py +67 -0
  482. package/opus/scripts/shrink_model.sh +7 -0
  483. package/opus/silk/A2NLSF.c +267 -0
  484. package/opus/silk/API.h +156 -0
  485. package/opus/silk/CNG.c +188 -0
  486. package/opus/silk/HP_variable_cutoff.c +77 -0
  487. package/opus/silk/Inlines.h +188 -0
  488. package/opus/silk/LPC_analysis_filter.c +111 -0
  489. package/opus/silk/LPC_fit.c +82 -0
  490. package/opus/silk/LPC_inv_pred_gain.c +141 -0
  491. package/opus/silk/LP_variable_cutoff.c +135 -0
  492. package/opus/silk/MacroCount.h +710 -0
  493. package/opus/silk/MacroDebug.h +945 -0
  494. package/opus/silk/NLSF2A.c +141 -0
  495. package/opus/silk/NLSF_VQ.c +76 -0
  496. package/opus/silk/NLSF_VQ_weights_laroia.c +80 -0
  497. package/opus/silk/NLSF_decode.c +93 -0
  498. package/opus/silk/NLSF_del_dec_quant.c +215 -0
  499. package/opus/silk/NLSF_encode.c +124 -0
  500. package/opus/silk/NLSF_stabilize.c +142 -0
  501. package/opus/silk/NLSF_unpack.c +55 -0
  502. package/opus/silk/NSQ.c +437 -0
  503. package/opus/silk/NSQ.h +101 -0
  504. package/opus/silk/NSQ_del_dec.c +733 -0
  505. package/opus/silk/PLC.c +493 -0
  506. package/opus/silk/PLC.h +65 -0
  507. package/opus/silk/SigProc_FIX.h +643 -0
  508. package/opus/silk/VAD.c +360 -0
  509. package/opus/silk/VQ_WMat_EC.c +131 -0
  510. package/opus/silk/ana_filt_bank_1.c +74 -0
  511. package/opus/silk/arm/LPC_inv_pred_gain_arm.h +57 -0
  512. package/opus/silk/arm/LPC_inv_pred_gain_neon_intr.c +288 -0
  513. package/opus/silk/arm/NSQ_del_dec_arm.h +100 -0
  514. package/opus/silk/arm/NSQ_del_dec_neon_intr.c +1138 -0
  515. package/opus/silk/arm/NSQ_neon.c +112 -0
  516. package/opus/silk/arm/NSQ_neon.h +114 -0
  517. package/opus/silk/arm/SigProc_FIX_armv4.h +47 -0
  518. package/opus/silk/arm/SigProc_FIX_armv5e.h +61 -0
  519. package/opus/silk/arm/arm_silk_map.c +128 -0
  520. package/opus/silk/arm/biquad_alt_arm.h +68 -0
  521. package/opus/silk/arm/biquad_alt_neon_intr.c +156 -0
  522. package/opus/silk/arm/macros_arm64.h +39 -0
  523. package/opus/silk/arm/macros_armv4.h +110 -0
  524. package/opus/silk/arm/macros_armv5e.h +220 -0
  525. package/opus/silk/biquad_alt.c +121 -0
  526. package/opus/silk/bwexpander.c +51 -0
  527. package/opus/silk/bwexpander_32.c +51 -0
  528. package/opus/silk/check_control_input.c +106 -0
  529. package/opus/silk/code_signs.c +115 -0
  530. package/opus/silk/control.h +161 -0
  531. package/opus/silk/control_SNR.c +113 -0
  532. package/opus/silk/control_audio_bandwidth.c +132 -0
  533. package/opus/silk/control_codec.c +423 -0
  534. package/opus/silk/debug.c +174 -0
  535. package/opus/silk/debug.h +267 -0
  536. package/opus/silk/dec_API.c +465 -0
  537. package/opus/silk/decode_core.c +243 -0
  538. package/opus/silk/decode_frame.c +169 -0
  539. package/opus/silk/decode_indices.c +151 -0
  540. package/opus/silk/decode_parameters.c +115 -0
  541. package/opus/silk/decode_pitch.c +77 -0
  542. package/opus/silk/decode_pulses.c +115 -0
  543. package/opus/silk/decoder_set_fs.c +108 -0
  544. package/opus/silk/define.h +235 -0
  545. package/opus/silk/enc_API.c +593 -0
  546. package/opus/silk/encode_indices.c +181 -0
  547. package/opus/silk/encode_pulses.c +206 -0
  548. package/opus/silk/errors.h +98 -0
  549. package/opus/silk/fixed/LTP_analysis_filter_FIX.c +90 -0
  550. package/opus/silk/fixed/LTP_scale_ctrl_FIX.c +58 -0
  551. package/opus/silk/fixed/apply_sine_window_FIX.c +101 -0
  552. package/opus/silk/fixed/arm/warped_autocorrelation_FIX_arm.h +68 -0
  553. package/opus/silk/fixed/arm/warped_autocorrelation_FIX_neon_intr.c +265 -0
  554. package/opus/silk/fixed/autocorr_FIX.c +48 -0
  555. package/opus/silk/fixed/burg_modified_FIX.c +280 -0
  556. package/opus/silk/fixed/corrMatrix_FIX.c +150 -0
  557. package/opus/silk/fixed/encode_frame_FIX.c +444 -0
  558. package/opus/silk/fixed/find_LPC_FIX.c +151 -0
  559. package/opus/silk/fixed/find_LTP_FIX.c +99 -0
  560. package/opus/silk/fixed/find_pitch_lags_FIX.c +143 -0
  561. package/opus/silk/fixed/find_pred_coefs_FIX.c +146 -0
  562. package/opus/silk/fixed/k2a_FIX.c +54 -0
  563. package/opus/silk/fixed/k2a_Q16_FIX.c +54 -0
  564. package/opus/silk/fixed/main_FIX.h +244 -0
  565. package/opus/silk/fixed/mips/noise_shape_analysis_FIX_mipsr1.h +336 -0
  566. package/opus/silk/fixed/mips/prefilter_FIX_mipsr1.h +184 -0
  567. package/opus/silk/fixed/mips/warped_autocorrelation_FIX_mipsr1.h +165 -0
  568. package/opus/silk/fixed/noise_shape_analysis_FIX.c +407 -0
  569. package/opus/silk/fixed/pitch_analysis_core_FIX.c +721 -0
  570. package/opus/silk/fixed/process_gains_FIX.c +117 -0
  571. package/opus/silk/fixed/regularize_correlations_FIX.c +47 -0
  572. package/opus/silk/fixed/residual_energy16_FIX.c +103 -0
  573. package/opus/silk/fixed/residual_energy_FIX.c +98 -0
  574. package/opus/silk/fixed/schur64_FIX.c +93 -0
  575. package/opus/silk/fixed/schur_FIX.c +107 -0
  576. package/opus/silk/fixed/structs_FIX.h +116 -0
  577. package/opus/silk/fixed/vector_ops_FIX.c +102 -0
  578. package/opus/silk/fixed/warped_autocorrelation_FIX.c +92 -0
  579. package/opus/silk/fixed/x86/burg_modified_FIX_sse4_1.c +400 -0
  580. package/opus/silk/fixed/x86/vector_ops_FIX_sse4_1.c +93 -0
  581. package/opus/silk/float/LPC_analysis_filter_FLP.c +249 -0
  582. package/opus/silk/float/LPC_inv_pred_gain_FLP.c +73 -0
  583. package/opus/silk/float/LTP_analysis_filter_FLP.c +75 -0
  584. package/opus/silk/float/LTP_scale_ctrl_FLP.c +58 -0
  585. package/opus/silk/float/SigProc_FLP.h +205 -0
  586. package/opus/silk/float/apply_sine_window_FLP.c +81 -0
  587. package/opus/silk/float/autocorrelation_FLP.c +53 -0
  588. package/opus/silk/float/burg_modified_FLP.c +187 -0
  589. package/opus/silk/float/bwexpander_FLP.c +49 -0
  590. package/opus/silk/float/corrMatrix_FLP.c +95 -0
  591. package/opus/silk/float/encode_frame_FLP.c +432 -0
  592. package/opus/silk/float/energy_FLP.c +59 -0
  593. package/opus/silk/float/find_LPC_FLP.c +105 -0
  594. package/opus/silk/float/find_LTP_FLP.c +65 -0
  595. package/opus/silk/float/find_pitch_lags_FLP.c +132 -0
  596. package/opus/silk/float/find_pred_coefs_FLP.c +117 -0
  597. package/opus/silk/float/inner_product_FLP.c +59 -0
  598. package/opus/silk/float/k2a_FLP.c +54 -0
  599. package/opus/silk/float/main_FLP.h +290 -0
  600. package/opus/silk/float/noise_shape_analysis_FLP.c +350 -0
  601. package/opus/silk/float/pitch_analysis_core_FLP.c +630 -0
  602. package/opus/silk/float/process_gains_FLP.c +103 -0
  603. package/opus/silk/float/regularize_correlations_FLP.c +48 -0
  604. package/opus/silk/float/residual_energy_FLP.c +117 -0
  605. package/opus/silk/float/scale_copy_vector_FLP.c +57 -0
  606. package/opus/silk/float/scale_vector_FLP.c +56 -0
  607. package/opus/silk/float/schur_FLP.c +70 -0
  608. package/opus/silk/float/sort_FLP.c +83 -0
  609. package/opus/silk/float/structs_FLP.h +112 -0
  610. package/opus/silk/float/warped_autocorrelation_FLP.c +75 -0
  611. package/opus/silk/float/wrappers_FLP.c +209 -0
  612. package/opus/silk/float/x86/inner_product_FLP_avx2.c +85 -0
  613. package/opus/silk/gain_quant.c +142 -0
  614. package/opus/silk/init_decoder.c +84 -0
  615. package/opus/silk/init_encoder.c +68 -0
  616. package/opus/silk/inner_prod_aligned.c +47 -0
  617. package/opus/silk/interpolate.c +51 -0
  618. package/opus/silk/lin2log.c +46 -0
  619. package/opus/silk/log2lin.c +58 -0
  620. package/opus/silk/macros.h +151 -0
  621. package/opus/silk/main.h +486 -0
  622. package/opus/silk/meson.build +69 -0
  623. package/opus/silk/mips/NSQ_del_dec_mipsr1.h +410 -0
  624. package/opus/silk/mips/macros_mipsr1.h +92 -0
  625. package/opus/silk/mips/sigproc_fix_mipsr1.h +60 -0
  626. package/opus/silk/pitch_est_defines.h +88 -0
  627. package/opus/silk/pitch_est_tables.c +99 -0
  628. package/opus/silk/process_NLSFs.c +107 -0
  629. package/opus/silk/quant_LTP_gains.c +132 -0
  630. package/opus/silk/resampler.c +215 -0
  631. package/opus/silk/resampler_down2.c +74 -0
  632. package/opus/silk/resampler_down2_3.c +103 -0
  633. package/opus/silk/resampler_private.h +88 -0
  634. package/opus/silk/resampler_private_AR2.c +55 -0
  635. package/opus/silk/resampler_private_IIR_FIR.c +107 -0
  636. package/opus/silk/resampler_private_down_FIR.c +194 -0
  637. package/opus/silk/resampler_private_up2_HQ.c +113 -0
  638. package/opus/silk/resampler_rom.c +96 -0
  639. package/opus/silk/resampler_rom.h +68 -0
  640. package/opus/silk/resampler_structs.h +60 -0
  641. package/opus/silk/shell_coder.c +151 -0
  642. package/opus/silk/sigm_Q15.c +76 -0
  643. package/opus/silk/sort.c +154 -0
  644. package/opus/silk/stereo_LR_to_MS.c +229 -0
  645. package/opus/silk/stereo_MS_to_LR.c +85 -0
  646. package/opus/silk/stereo_decode_pred.c +73 -0
  647. package/opus/silk/stereo_encode_pred.c +62 -0
  648. package/opus/silk/stereo_find_predictor.c +79 -0
  649. package/opus/silk/stereo_quant_pred.c +73 -0
  650. package/opus/silk/structs.h +357 -0
  651. package/opus/silk/sum_sqr_shift.c +83 -0
  652. package/opus/silk/table_LSF_cos.c +70 -0
  653. package/opus/silk/tables.h +114 -0
  654. package/opus/silk/tables_LTP.c +294 -0
  655. package/opus/silk/tables_NLSF_CB_NB_MB.c +195 -0
  656. package/opus/silk/tables_NLSF_CB_WB.c +234 -0
  657. package/opus/silk/tables_gain.c +63 -0
  658. package/opus/silk/tables_other.c +124 -0
  659. package/opus/silk/tables_pitch_lag.c +69 -0
  660. package/opus/silk/tables_pulses_per_block.c +264 -0
  661. package/opus/silk/tests/meson.build +8 -0
  662. package/opus/silk/tests/test_unit_LPC_inv_pred_gain.c +130 -0
  663. package/opus/silk/tuning_parameters.h +155 -0
  664. package/opus/silk/typedef.h +81 -0
  665. package/opus/silk/x86/NSQ_del_dec_avx2.c +1075 -0
  666. package/opus/silk/x86/NSQ_del_dec_sse4_1.c +915 -0
  667. package/opus/silk/x86/NSQ_sse4_1.c +772 -0
  668. package/opus/silk/x86/SigProc_FIX_sse.h +99 -0
  669. package/opus/silk/x86/VAD_sse4_1.c +289 -0
  670. package/opus/silk/x86/VQ_WMat_EC_sse4_1.c +173 -0
  671. package/opus/silk/x86/main_sse.h +299 -0
  672. package/opus/silk/x86/x86_silk_map.c +179 -0
  673. package/opus/silk_headers.mk +44 -0
  674. package/opus/silk_sources.mk +164 -0
  675. package/opus/src/analysis.c +979 -0
  676. package/opus/src/analysis.h +103 -0
  677. package/opus/src/extensions.c +663 -0
  678. package/opus/src/mapping_matrix.c +993 -0
  679. package/opus/src/mapping_matrix.h +165 -0
  680. package/opus/src/meson.build +45 -0
  681. package/opus/src/mlp.c +132 -0
  682. package/opus/src/mlp.h +60 -0
  683. package/opus/src/mlp_data.c +672 -0
  684. package/opus/src/opus.c +362 -0
  685. package/opus/src/opus_compare.c +382 -0
  686. package/opus/src/opus_decoder.c +1579 -0
  687. package/opus/src/opus_demo.c +1194 -0
  688. package/opus/src/opus_encoder.c +3180 -0
  689. package/opus/src/opus_multistream.c +92 -0
  690. package/opus/src/opus_multistream_decoder.c +563 -0
  691. package/opus/src/opus_multistream_encoder.c +1321 -0
  692. package/opus/src/opus_private.h +263 -0
  693. package/opus/src/opus_projection_decoder.c +283 -0
  694. package/opus/src/opus_projection_encoder.c +522 -0
  695. package/opus/src/repacketizer.c +469 -0
  696. package/opus/src/repacketizer_demo.c +247 -0
  697. package/opus/src/tansig_table.h +45 -0
  698. package/opus/tar_list.txt +28 -0
  699. package/opus/tests/meson.build +39 -0
  700. package/opus/tests/opus_build_test.sh +29 -0
  701. package/opus/tests/opus_decode_fuzzer.c +124 -0
  702. package/opus/tests/opus_decode_fuzzer.options +2 -0
  703. package/opus/tests/opus_encode_regressions.c +1034 -0
  704. package/opus/tests/random_config.sh +126 -0
  705. package/opus/tests/run_vectors.sh +143 -0
  706. package/opus/tests/test_opus_api.c +1904 -0
  707. package/opus/tests/test_opus_common.h +86 -0
  708. package/opus/tests/test_opus_custom.c +694 -0
  709. package/opus/tests/test_opus_decode.c +481 -0
  710. package/opus/tests/test_opus_dred.c +107 -0
  711. package/opus/tests/test_opus_encode.c +738 -0
  712. package/opus/tests/test_opus_extensions.c +750 -0
  713. package/opus/tests/test_opus_padding.c +94 -0
  714. package/opus/tests/test_opus_projection.c +384 -0
  715. package/opus/training/rnn_dump.py +66 -0
  716. package/opus/training/rnn_train.py +177 -0
  717. package/opus/training/txt2hdf5.py +12 -0
  718. package/opus/update_version +65 -0
  719. package/package.json +2 -2
  720. package/src/build-ffmpeg-msys.sh +8 -2
@@ -0,0 +1,2854 @@
1
+ /* Copyright (c) 2007-2008 CSIRO
2
+ Copyright (c) 2007-2010 Xiph.Org Foundation
3
+ Copyright (c) 2008 Gregory Maxwell
4
+ Written by Jean-Marc Valin and Gregory Maxwell */
5
+ /*
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions
8
+ are met:
9
+
10
+ - Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+
13
+ - Redistributions in binary form must reproduce the above copyright
14
+ notice, this list of conditions and the following disclaimer in the
15
+ documentation and/or other materials provided with the distribution.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ */
29
+
30
+ #ifdef HAVE_CONFIG_H
31
+ #include "config.h"
32
+ #endif
33
+
34
+ #define CELT_ENCODER_C
35
+
36
+ #include "cpu_support.h"
37
+ #include "os_support.h"
38
+ #include "mdct.h"
39
+ #include <math.h>
40
+ #include "celt.h"
41
+ #include "pitch.h"
42
+ #include "bands.h"
43
+ #include "modes.h"
44
+ #include "entcode.h"
45
+ #include "quant_bands.h"
46
+ #include "rate.h"
47
+ #include "stack_alloc.h"
48
+ #include "mathops.h"
49
+ #include "float_cast.h"
50
+ #include <stdarg.h>
51
+ #include "celt_lpc.h"
52
+ #include "vq.h"
53
+
54
+
55
+ #ifndef M_PI
56
+ #define M_PI 3.141592653
57
+ #endif
58
+
59
+
60
+ /** Encoder state
61
+ @brief Encoder state
62
+ */
63
+ struct OpusCustomEncoder {
64
+ const OpusCustomMode *mode; /**< Mode used by the encoder */
65
+ int channels;
66
+ int stream_channels;
67
+
68
+ int force_intra;
69
+ int clip;
70
+ int disable_pf;
71
+ int complexity;
72
+ int upsample;
73
+ int start, end;
74
+
75
+ opus_int32 bitrate;
76
+ int vbr;
77
+ int signalling;
78
+ int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
79
+ int loss_rate;
80
+ int lsb_depth;
81
+ int lfe;
82
+ int disable_inv;
83
+ int arch;
84
+
85
+ /* Everything beyond this point gets cleared on a reset */
86
+ #define ENCODER_RESET_START rng
87
+
88
+ opus_uint32 rng;
89
+ int spread_decision;
90
+ opus_val32 delayedIntra;
91
+ int tonal_average;
92
+ int lastCodedBands;
93
+ int hf_average;
94
+ int tapset_decision;
95
+
96
+ int prefilter_period;
97
+ opus_val16 prefilter_gain;
98
+ int prefilter_tapset;
99
+ #ifdef RESYNTH
100
+ int prefilter_period_old;
101
+ opus_val16 prefilter_gain_old;
102
+ int prefilter_tapset_old;
103
+ #endif
104
+ int consec_transient;
105
+ AnalysisInfo analysis;
106
+ SILKInfo silk_info;
107
+
108
+ opus_val32 preemph_memE[2];
109
+ opus_val32 preemph_memD[2];
110
+
111
+ /* VBR-related parameters */
112
+ opus_int32 vbr_reservoir;
113
+ opus_int32 vbr_drift;
114
+ opus_int32 vbr_offset;
115
+ opus_int32 vbr_count;
116
+ opus_val32 overlap_max;
117
+ opus_val16 stereo_saving;
118
+ int intensity;
119
+ celt_glog *energy_mask;
120
+ celt_glog spec_avg;
121
+
122
+ #ifdef RESYNTH
123
+ /* +MAX_PERIOD/2 to make space for overlap */
124
+ celt_sig syn_mem[2][2*MAX_PERIOD+MAX_PERIOD/2];
125
+ #endif
126
+
127
+ celt_sig in_mem[1]; /* Size = channels*mode->overlap */
128
+ /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */
129
+ /* celt_glog oldBandE[], Size = channels*mode->nbEBands */
130
+ /* celt_glog oldLogE[], Size = channels*mode->nbEBands */
131
+ /* celt_glog oldLogE2[], Size = channels*mode->nbEBands */
132
+ /* celt_glog energyError[], Size = channels*mode->nbEBands */
133
+ };
134
+
135
+ int celt_encoder_get_size(int channels)
136
+ {
137
+ CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
138
+ return opus_custom_encoder_get_size(mode, channels);
139
+ }
140
+
141
+ OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
142
+ {
143
+ int size = sizeof(struct CELTEncoder)
144
+ + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
145
+ + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
146
+ + 4*channels*mode->nbEBands*sizeof(celt_glog); /* celt_glog oldBandE[channels*mode->nbEBands]; */
147
+ /* celt_glog oldLogE[channels*mode->nbEBands]; */
148
+ /* celt_glog oldLogE2[channels*mode->nbEBands]; */
149
+ /* celt_glog energyError[channels*mode->nbEBands]; */
150
+ return size;
151
+ }
152
+
153
+ #ifdef CUSTOM_MODES
154
+ CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
155
+ {
156
+ int ret;
157
+ CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
158
+ /* init will handle the NULL case */
159
+ ret = opus_custom_encoder_init(st, mode, channels);
160
+ if (ret != OPUS_OK)
161
+ {
162
+ opus_custom_encoder_destroy(st);
163
+ st = NULL;
164
+ }
165
+ if (error)
166
+ *error = ret;
167
+ return st;
168
+ }
169
+ #endif /* CUSTOM_MODES */
170
+
171
+ static int opus_custom_encoder_init_arch(CELTEncoder *st, const CELTMode *mode,
172
+ int channels, int arch)
173
+ {
174
+ if (channels < 0 || channels > 2)
175
+ return OPUS_BAD_ARG;
176
+
177
+ if (st==NULL || mode==NULL)
178
+ return OPUS_ALLOC_FAIL;
179
+
180
+ OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
181
+
182
+ st->mode = mode;
183
+ st->stream_channels = st->channels = channels;
184
+
185
+ st->upsample = 1;
186
+ st->start = 0;
187
+ st->end = st->mode->effEBands;
188
+ st->signalling = 1;
189
+ st->arch = arch;
190
+
191
+ st->constrained_vbr = 1;
192
+ st->clip = 1;
193
+
194
+ st->bitrate = OPUS_BITRATE_MAX;
195
+ st->vbr = 0;
196
+ st->force_intra = 0;
197
+ st->complexity = 5;
198
+ st->lsb_depth=24;
199
+
200
+ opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
201
+
202
+ return OPUS_OK;
203
+ }
204
+
205
+ #ifdef CUSTOM_MODES
206
+ int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
207
+ {
208
+ return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch());
209
+ }
210
+ #endif
211
+
212
+ int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
213
+ int arch)
214
+ {
215
+ int ret;
216
+ ret = opus_custom_encoder_init_arch(st,
217
+ opus_custom_mode_create(48000, 960, NULL), channels, arch);
218
+ if (ret != OPUS_OK)
219
+ return ret;
220
+ st->upsample = resampling_factor(sampling_rate);
221
+ return OPUS_OK;
222
+ }
223
+
224
+ #ifdef CUSTOM_MODES
225
+ void opus_custom_encoder_destroy(CELTEncoder *st)
226
+ {
227
+ opus_free(st);
228
+ }
229
+ #endif /* CUSTOM_MODES */
230
+
231
+
232
+ static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
233
+ opus_val16 *tf_estimate, int *tf_chan, int allow_weak_transients,
234
+ int *weak_transient, opus_val16 tone_freq, opus_val32 toneishness)
235
+ {
236
+ int i;
237
+ VARDECL(opus_val16, tmp);
238
+ opus_val32 mem0,mem1;
239
+ int is_transient = 0;
240
+ opus_int32 mask_metric = 0;
241
+ int c;
242
+ opus_val16 tf_max;
243
+ int len2;
244
+ /* Forward masking: 6.7 dB/ms. */
245
+ #ifdef FIXED_POINT
246
+ int forward_shift = 4;
247
+ #else
248
+ opus_val16 forward_decay = QCONST16(.0625f,15);
249
+ #endif
250
+ /* Table of 6*64/x, trained on real data to minimize the average error */
251
+ static const unsigned char inv_table[128] = {
252
+ 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
253
+ 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
254
+ 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8,
255
+ 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
256
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
257
+ 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
258
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
259
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
260
+ };
261
+ SAVE_STACK;
262
+ ALLOC(tmp, len, opus_val16);
263
+
264
+ *weak_transient = 0;
265
+ /* For lower bitrates, let's be more conservative and have a forward masking
266
+ decay of 3.3 dB/ms. This avoids having to code transients at very low
267
+ bitrate (mostly for hybrid), which can result in unstable energy and/or
268
+ partial collapse. */
269
+ if (allow_weak_transients)
270
+ {
271
+ #ifdef FIXED_POINT
272
+ forward_shift = 5;
273
+ #else
274
+ forward_decay = QCONST16(.03125f,15);
275
+ #endif
276
+ }
277
+ len2=len/2;
278
+ for (c=0;c<C;c++)
279
+ {
280
+ opus_val32 mean;
281
+ opus_int32 unmask=0;
282
+ opus_val32 norm;
283
+ opus_val16 maxE;
284
+ mem0=0;
285
+ mem1=0;
286
+ /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
287
+ for (i=0;i<len;i++)
288
+ {
289
+ #ifndef FIXED_POINT
290
+ float mem00;
291
+ #endif
292
+ opus_val32 x,y;
293
+ x = SHR32(in[i+c*len],SIG_SHIFT);
294
+ y = ADD32(mem0, x);
295
+ #ifdef FIXED_POINT
296
+ mem0 = mem1 + y - SHL32(x,1);
297
+ mem1 = x - SHR32(y,1);
298
+ #else
299
+ /* Original code:
300
+ mem0 = mem1 + y - 2*x;
301
+ mem1 = x - .5f*y;
302
+ Modified code to shorten dependency chains: */
303
+ mem00=mem0;
304
+ mem0 = mem0 - x + .5f*mem1;
305
+ mem1 = x - mem00;
306
+ #endif
307
+ tmp[i] = SROUND16(y, 2);
308
+ /*printf("%f ", tmp[i]);*/
309
+ }
310
+ /*printf("\n");*/
311
+ /* First few samples are bad because we don't propagate the memory */
312
+ OPUS_CLEAR(tmp, 12);
313
+
314
+ #ifdef FIXED_POINT
315
+ /* Normalize tmp to max range */
316
+ {
317
+ int shift=0;
318
+ shift = 14-celt_ilog2(MAX16(1, celt_maxabs16(tmp, len)));
319
+ if (shift!=0)
320
+ {
321
+ for (i=0;i<len;i++)
322
+ tmp[i] = SHL16(tmp[i], shift);
323
+ }
324
+ }
325
+ #endif
326
+
327
+ mean=0;
328
+ mem0=0;
329
+ /* Grouping by two to reduce complexity */
330
+ /* Forward pass to compute the post-echo threshold*/
331
+ for (i=0;i<len2;i++)
332
+ {
333
+ opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),16);
334
+ mean += x2;
335
+ #ifdef FIXED_POINT
336
+ /* FIXME: Use PSHR16() instead */
337
+ tmp[i] = mem0 + PSHR32(x2-mem0,forward_shift);
338
+ mem0 = tmp[i];
339
+ #else
340
+ mem0 = x2 + (1.f-forward_decay)*mem0;
341
+ tmp[i] = forward_decay*mem0;
342
+ #endif
343
+ }
344
+
345
+ mem0=0;
346
+ maxE=0;
347
+ /* Backward pass to compute the pre-echo threshold */
348
+ for (i=len2-1;i>=0;i--)
349
+ {
350
+ /* Backward masking: 13.9 dB/ms. */
351
+ #ifdef FIXED_POINT
352
+ /* FIXME: Use PSHR16() instead */
353
+ tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3);
354
+ mem0 = tmp[i];
355
+ maxE = MAX16(maxE, mem0);
356
+ #else
357
+ mem0 = tmp[i] + 0.875f*mem0;
358
+ tmp[i] = 0.125f*mem0;
359
+ maxE = MAX16(maxE, 0.125f*mem0);
360
+ #endif
361
+ }
362
+ /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
363
+
364
+ /* Compute the ratio of the "frame energy" over the harmonic mean of the energy.
365
+ This essentially corresponds to a bitrate-normalized temporal noise-to-mask
366
+ ratio */
367
+
368
+ /* As a compromise with the old transient detector, frame energy is the
369
+ geometric mean of the energy and half the max */
370
+ #ifdef FIXED_POINT
371
+ /* Costs two sqrt() to avoid overflows */
372
+ mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
373
+ #else
374
+ mean = celt_sqrt(mean * maxE*.5*len2);
375
+ #endif
376
+ /* Inverse of the mean energy in Q15+6 */
377
+ norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1));
378
+ /* Compute harmonic mean discarding the unreliable boundaries
379
+ The data is smooth, so we only take 1/4th of the samples */
380
+ unmask=0;
381
+ /* We should never see NaNs here. If we find any, then something really bad happened and we better abort
382
+ before it does any damage later on. If these asserts are disabled (no hardening), then the table
383
+ lookup a few lines below (id = ...) is likely to crash dur to an out-of-bounds read. DO NOT FIX
384
+ that crash on NaN since it could result in a worse issue later on. */
385
+ celt_assert(!celt_isnan(tmp[0]));
386
+ celt_assert(!celt_isnan(norm));
387
+ for (i=12;i<len2-5;i+=4)
388
+ {
389
+ int id;
390
+ #ifdef FIXED_POINT
391
+ id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm))); /* Do not round to nearest */
392
+ #else
393
+ id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
394
+ #endif
395
+ unmask += inv_table[id];
396
+ }
397
+ /*printf("%d\n", unmask);*/
398
+ /* Normalize, compensate for the 1/4th of the sample and the factor of 6 in the inverse table */
399
+ unmask = 64*unmask*4/(6*(len2-17));
400
+ if (unmask>mask_metric)
401
+ {
402
+ *tf_chan = c;
403
+ mask_metric = unmask;
404
+ }
405
+ }
406
+ is_transient = mask_metric>200;
407
+ /* Prevent the transient detector from confusing the partial cycle of a
408
+ very low frequency tone with a transient. */
409
+ if (toneishness > QCONST32(.98f, 29) && tone_freq < QCONST16(0.026f, 13))
410
+ is_transient = 0;
411
+ /* For low bitrates, define "weak transients" that need to be
412
+ handled differently to avoid partial collapse. */
413
+ if (allow_weak_transients && is_transient && mask_metric<600) {
414
+ is_transient = 0;
415
+ *weak_transient = 1;
416
+ }
417
+ /* Arbitrary metric for VBR boost */
418
+ tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
419
+ /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
420
+ *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
421
+ /*printf("%d %f\n", tf_max, mask_metric);*/
422
+ RESTORE_STACK;
423
+ #ifdef FUZZING
424
+ is_transient = rand()&0x1;
425
+ #endif
426
+ /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
427
+ return is_transient;
428
+ }
429
+
430
+ /* Looks for sudden increases of energy to decide whether we need to patch
431
+ the transient decision */
432
+ static int patch_transient_decision(celt_glog *newE, celt_glog *oldE, int nbEBands,
433
+ int start, int end, int C)
434
+ {
435
+ int i, c;
436
+ opus_val32 mean_diff=0;
437
+ celt_glog spread_old[26];
438
+ /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
439
+ avoid false detection caused by irrelevant bands */
440
+ if (C==1)
441
+ {
442
+ spread_old[start] = oldE[start];
443
+ for (i=start+1;i<end;i++)
444
+ spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f), oldE[i]);
445
+ } else {
446
+ spread_old[start] = MAXG(oldE[start],oldE[start+nbEBands]);
447
+ for (i=start+1;i<end;i++)
448
+ spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f),
449
+ MAXG(oldE[i],oldE[i+nbEBands]));
450
+ }
451
+ for (i=end-2;i>=start;i--)
452
+ spread_old[i] = MAXG(spread_old[i], spread_old[i+1]-GCONST(1.0f));
453
+ /* Compute mean increase */
454
+ c=0; do {
455
+ for (i=IMAX(2,start);i<end-1;i++)
456
+ {
457
+ opus_val16 x1, x2;
458
+ x1 = MAXG(0, newE[i + c*nbEBands]);
459
+ x2 = MAXG(0, spread_old[i]);
460
+ mean_diff = ADD32(mean_diff, MAXG(0, SUB32(x1, x2)));
461
+ }
462
+ } while (++c<C);
463
+ mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
464
+ /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
465
+ return mean_diff > GCONST(1.f);
466
+ }
467
+
468
+ /** Apply window and compute the MDCT for all sub-frames and
469
+ all channels in a frame */
470
+ static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
471
+ celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample,
472
+ int arch)
473
+ {
474
+ const int overlap = mode->overlap;
475
+ int N;
476
+ int B;
477
+ int shift;
478
+ int i, b, c;
479
+ if (shortBlocks)
480
+ {
481
+ B = shortBlocks;
482
+ N = mode->shortMdctSize;
483
+ shift = mode->maxLM;
484
+ } else {
485
+ B = 1;
486
+ N = mode->shortMdctSize<<LM;
487
+ shift = mode->maxLM-LM;
488
+ }
489
+ c=0; do {
490
+ for (b=0;b<B;b++)
491
+ {
492
+ /* Interleaving the sub-frames while doing the MDCTs */
493
+ clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,
494
+ &out[b+c*N*B], mode->window, overlap, shift, B,
495
+ arch);
496
+ }
497
+ } while (++c<CC);
498
+ if (CC==2&&C==1)
499
+ {
500
+ for (i=0;i<B*N;i++)
501
+ out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
502
+ }
503
+ if (upsample != 1)
504
+ {
505
+ c=0; do
506
+ {
507
+ int bound = B*N/upsample;
508
+ for (i=0;i<bound;i++)
509
+ out[c*B*N+i] *= upsample;
510
+ OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
511
+ } while (++c<C);
512
+ }
513
+ }
514
+
515
+
516
+ void celt_preemphasis(const opus_res * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
517
+ int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
518
+ {
519
+ int i;
520
+ opus_val16 coef0;
521
+ celt_sig m;
522
+ int Nu;
523
+
524
+ coef0 = coef[0];
525
+ m = *mem;
526
+
527
+ /* Fast path for the normal 48kHz case and no clipping */
528
+ if (coef[1] == 0 && upsample == 1 && !clip)
529
+ {
530
+ for (i=0;i<N;i++)
531
+ {
532
+ celt_sig x;
533
+ x = RES2SIG(pcmp[CC*i]);
534
+ /* Apply pre-emphasis */
535
+ inp[i] = x - m;
536
+ m = MULT16_32_Q15(coef0, x);
537
+ }
538
+ *mem = m;
539
+ return;
540
+ }
541
+
542
+ Nu = N/upsample;
543
+ if (upsample!=1)
544
+ {
545
+ OPUS_CLEAR(inp, N);
546
+ }
547
+ for (i=0;i<Nu;i++)
548
+ inp[i*upsample] = RES2SIG(pcmp[CC*i]);
549
+
550
+ #ifndef FIXED_POINT
551
+ if (clip)
552
+ {
553
+ /* Clip input to avoid encoding non-portable files */
554
+ for (i=0;i<Nu;i++)
555
+ inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
556
+ }
557
+ #else
558
+ (void)clip; /* Avoids a warning about clip being unused. */
559
+ #endif
560
+ #ifdef CUSTOM_MODES
561
+ if (coef[1] != 0)
562
+ {
563
+ opus_val16 coef1 = coef[1];
564
+ opus_val16 coef2 = coef[2];
565
+ for (i=0;i<N;i++)
566
+ {
567
+ celt_sig x, tmp;
568
+ x = inp[i];
569
+ /* Apply pre-emphasis */
570
+ tmp = SHL32(MULT16_32_Q15(coef2, x), 15-SIG_SHIFT);
571
+ inp[i] = tmp + m;
572
+ m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
573
+ }
574
+ } else
575
+ #endif
576
+ {
577
+ for (i=0;i<N;i++)
578
+ {
579
+ celt_sig x;
580
+ x = inp[i];
581
+ /* Apply pre-emphasis */
582
+ inp[i] = x - m;
583
+ m = MULT16_32_Q15(coef0, x);
584
+ }
585
+ }
586
+ *mem = m;
587
+ }
588
+
589
+
590
+
591
+ static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
592
+ {
593
+ int i;
594
+ opus_val32 L1;
595
+ L1 = 0;
596
+ for (i=0;i<N;i++)
597
+ L1 += EXTEND32(ABS16(tmp[i]));
598
+ /* When in doubt, prefer good freq resolution */
599
+ L1 = MAC16_32_Q15(L1, LM*bias, L1);
600
+ return L1;
601
+
602
+ }
603
+
604
+ static int tf_analysis(const CELTMode *m, int len, int isTransient,
605
+ int *tf_res, int lambda, celt_norm *X, int N0, int LM,
606
+ opus_val16 tf_estimate, int tf_chan, int *importance)
607
+ {
608
+ int i;
609
+ VARDECL(int, metric);
610
+ int cost0;
611
+ int cost1;
612
+ VARDECL(int, path0);
613
+ VARDECL(int, path1);
614
+ VARDECL(celt_norm, tmp);
615
+ VARDECL(celt_norm, tmp_1);
616
+ int sel;
617
+ int selcost[2];
618
+ int tf_select=0;
619
+ opus_val16 bias;
620
+
621
+ SAVE_STACK;
622
+ bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
623
+ /*printf("%f ", bias);*/
624
+
625
+ ALLOC(metric, len, int);
626
+ ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
627
+ ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
628
+ ALLOC(path0, len, int);
629
+ ALLOC(path1, len, int);
630
+
631
+ for (i=0;i<len;i++)
632
+ {
633
+ int k, N;
634
+ int narrow;
635
+ opus_val32 L1, best_L1;
636
+ int best_level=0;
637
+ N = (m->eBands[i+1]-m->eBands[i])<<LM;
638
+ /* band is too narrow to be split down to LM=-1 */
639
+ narrow = (m->eBands[i+1]-m->eBands[i])==1;
640
+ OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
641
+ /* Just add the right channel if we're in stereo */
642
+ /*if (C==2)
643
+ for (j=0;j<N;j++)
644
+ tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
645
+ L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
646
+ best_L1 = L1;
647
+ /* Check the -1 case for transients */
648
+ if (isTransient && !narrow)
649
+ {
650
+ OPUS_COPY(tmp_1, tmp, N);
651
+ haar1(tmp_1, N>>LM, 1<<LM);
652
+ L1 = l1_metric(tmp_1, N, LM+1, bias);
653
+ if (L1<best_L1)
654
+ {
655
+ best_L1 = L1;
656
+ best_level = -1;
657
+ }
658
+ }
659
+ /*printf ("%f ", L1);*/
660
+ for (k=0;k<LM+!(isTransient||narrow);k++)
661
+ {
662
+ int B;
663
+
664
+ if (isTransient)
665
+ B = (LM-k-1);
666
+ else
667
+ B = k+1;
668
+
669
+ haar1(tmp, N>>k, 1<<k);
670
+
671
+ L1 = l1_metric(tmp, N, B, bias);
672
+
673
+ if (L1 < best_L1)
674
+ {
675
+ best_L1 = L1;
676
+ best_level = k+1;
677
+ }
678
+ }
679
+ /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
680
+ /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
681
+ if (isTransient)
682
+ metric[i] = 2*best_level;
683
+ else
684
+ metric[i] = -2*best_level;
685
+ /* For bands that can't be split to -1, set the metric to the half-way point to avoid
686
+ biasing the decision */
687
+ if (narrow && (metric[i]==0 || metric[i]==-2*LM))
688
+ metric[i]-=1;
689
+ /*printf("%d ", metric[i]/2 + (!isTransient)*LM);*/
690
+ }
691
+ /*printf("\n");*/
692
+ /* Search for the optimal tf resolution, including tf_select */
693
+ tf_select = 0;
694
+ for (sel=0;sel<2;sel++)
695
+ {
696
+ cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
697
+ cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+1]) + (isTransient ? 0 : lambda);
698
+ for (i=1;i<len;i++)
699
+ {
700
+ int curr0, curr1;
701
+ curr0 = IMIN(cost0, cost1 + lambda);
702
+ curr1 = IMIN(cost0 + lambda, cost1);
703
+ cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
704
+ cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
705
+ }
706
+ cost0 = IMIN(cost0, cost1);
707
+ selcost[sel]=cost0;
708
+ }
709
+ /* For now, we're conservative and only allow tf_select=1 for transients.
710
+ * If tests confirm it's useful for non-transients, we could allow it. */
711
+ if (selcost[1]<selcost[0] && isTransient)
712
+ tf_select=1;
713
+ cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
714
+ cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]) + (isTransient ? 0 : lambda);
715
+ /* Viterbi forward pass */
716
+ for (i=1;i<len;i++)
717
+ {
718
+ int curr0, curr1;
719
+ int from0, from1;
720
+
721
+ from0 = cost0;
722
+ from1 = cost1 + lambda;
723
+ if (from0 < from1)
724
+ {
725
+ curr0 = from0;
726
+ path0[i]= 0;
727
+ } else {
728
+ curr0 = from1;
729
+ path0[i]= 1;
730
+ }
731
+
732
+ from0 = cost0 + lambda;
733
+ from1 = cost1;
734
+ if (from0 < from1)
735
+ {
736
+ curr1 = from0;
737
+ path1[i]= 0;
738
+ } else {
739
+ curr1 = from1;
740
+ path1[i]= 1;
741
+ }
742
+ cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
743
+ cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
744
+ }
745
+ tf_res[len-1] = cost0 < cost1 ? 0 : 1;
746
+ /* Viterbi backward pass to check the decisions */
747
+ for (i=len-2;i>=0;i--)
748
+ {
749
+ if (tf_res[i+1] == 1)
750
+ tf_res[i] = path1[i+1];
751
+ else
752
+ tf_res[i] = path0[i+1];
753
+ }
754
+ /*printf("%d %f\n", *tf_sum, tf_estimate);*/
755
+ RESTORE_STACK;
756
+ #ifdef FUZZING
757
+ tf_select = rand()&0x1;
758
+ tf_res[0] = rand()&0x1;
759
+ for (i=1;i<len;i++)
760
+ tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
761
+ #endif
762
+ return tf_select;
763
+ }
764
+
765
+ static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
766
+ {
767
+ int curr, i;
768
+ int tf_select_rsv;
769
+ int tf_changed;
770
+ int logp;
771
+ opus_uint32 budget;
772
+ opus_uint32 tell;
773
+ budget = enc->storage*8;
774
+ tell = ec_tell(enc);
775
+ logp = isTransient ? 2 : 4;
776
+ /* Reserve space to code the tf_select decision. */
777
+ tf_select_rsv = LM>0 && tell+logp+1 <= budget;
778
+ budget -= tf_select_rsv;
779
+ curr = tf_changed = 0;
780
+ for (i=start;i<end;i++)
781
+ {
782
+ if (tell+logp<=budget)
783
+ {
784
+ ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
785
+ tell = ec_tell(enc);
786
+ curr = tf_res[i];
787
+ tf_changed |= curr;
788
+ }
789
+ else
790
+ tf_res[i] = curr;
791
+ logp = isTransient ? 4 : 5;
792
+ }
793
+ /* Only code tf_select if it would actually make a difference. */
794
+ if (tf_select_rsv &&
795
+ tf_select_table[LM][4*isTransient+0+tf_changed]!=
796
+ tf_select_table[LM][4*isTransient+2+tf_changed])
797
+ ec_enc_bit_logp(enc, tf_select, 1);
798
+ else
799
+ tf_select = 0;
800
+ for (i=start;i<end;i++)
801
+ tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
802
+ /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
803
+ }
804
+
805
+
806
+ static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
807
+ const celt_glog *bandLogE, int end, int LM, int C, int N0,
808
+ AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
809
+ int intensity, celt_glog surround_trim, opus_int32 equiv_rate, int arch)
810
+ {
811
+ int i;
812
+ opus_val32 diff=0;
813
+ int c;
814
+ int trim_index;
815
+ opus_val16 trim = QCONST16(5.f, 8);
816
+ opus_val16 logXC, logXC2;
817
+ /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
818
+ clear what's best, so we're keeping it as it was before, at least for now. */
819
+ if (equiv_rate < 64000) {
820
+ trim = QCONST16(4.f, 8);
821
+ } else if (equiv_rate < 80000) {
822
+ opus_int32 frac = (equiv_rate-64000) >> 10;
823
+ trim = QCONST16(4.f, 8) + QCONST16(1.f/16.f, 8)*frac;
824
+ }
825
+ if (C==2)
826
+ {
827
+ opus_val16 sum = 0; /* Q10 */
828
+ opus_val16 minXC; /* Q10 */
829
+ /* Compute inter-channel correlation for low frequencies */
830
+ for (i=0;i<8;i++)
831
+ {
832
+ opus_val32 partial;
833
+ partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
834
+ (m->eBands[i+1]-m->eBands[i])<<LM, arch);
835
+ sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
836
+ }
837
+ sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
838
+ sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
839
+ minXC = sum;
840
+ for (i=8;i<intensity;i++)
841
+ {
842
+ opus_val32 partial;
843
+ partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
844
+ (m->eBands[i+1]-m->eBands[i])<<LM, arch);
845
+ minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
846
+ }
847
+ minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
848
+ /*printf ("%f\n", sum);*/
849
+ /* mid-side savings estimations based on the LF average*/
850
+ logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
851
+ /* mid-side savings estimations based on min correlation */
852
+ logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
853
+ #ifdef FIXED_POINT
854
+ /* Compensate for Q20 vs Q14 input and convert output to Q8 */
855
+ logXC = PSHR32(logXC-QCONST16(6.f, 10),10-8);
856
+ logXC2 = PSHR32(logXC2-QCONST16(6.f, 10),10-8);
857
+ #endif
858
+
859
+ trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
860
+ *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
861
+ }
862
+
863
+ /* Estimate spectral tilt */
864
+ c=0; do {
865
+ for (i=0;i<end-1;i++)
866
+ {
867
+ diff += SHR32(bandLogE[i+c*m->nbEBands], 5)*(opus_int32)(2+2*i-end);
868
+ }
869
+ } while (++c<C);
870
+ diff /= C*(end-1);
871
+ /*printf("%f\n", diff);*/
872
+ trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST32(1.f, DB_SHIFT-5),DB_SHIFT-13)/6 ));
873
+ trim -= SHR16(surround_trim, DB_SHIFT-8);
874
+ trim -= 2*SHR16(tf_estimate, 14-8);
875
+ #ifndef DISABLE_FLOAT_API
876
+ if (analysis->valid)
877
+ {
878
+ trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
879
+ (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
880
+ }
881
+ #else
882
+ (void)analysis;
883
+ #endif
884
+
885
+ #ifdef FIXED_POINT
886
+ trim_index = PSHR32(trim, 8);
887
+ #else
888
+ trim_index = (int)floor(.5f+trim);
889
+ #endif
890
+ trim_index = IMAX(0, IMIN(10, trim_index));
891
+ /*printf("%d\n", trim_index);*/
892
+ #ifdef FUZZING
893
+ trim_index = rand()%11;
894
+ #endif
895
+ return trim_index;
896
+ }
897
+
898
+ static int stereo_analysis(const CELTMode *m, const celt_norm *X,
899
+ int LM, int N0)
900
+ {
901
+ int i;
902
+ int thetas;
903
+ opus_val32 sumLR = EPSILON, sumMS = EPSILON;
904
+
905
+ /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
906
+ for (i=0;i<13;i++)
907
+ {
908
+ int j;
909
+ for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
910
+ {
911
+ opus_val32 L, R, M, S;
912
+ /* We cast to 32-bit first because of the -32768 case */
913
+ L = EXTEND32(X[j]);
914
+ R = EXTEND32(X[N0+j]);
915
+ M = ADD32(L, R);
916
+ S = SUB32(L, R);
917
+ sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
918
+ sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
919
+ }
920
+ }
921
+ sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
922
+ thetas = 13;
923
+ /* We don't need thetas for lower bands with LM<=1 */
924
+ if (LM<=1)
925
+ thetas -= 8;
926
+ return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
927
+ > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
928
+ }
929
+
930
+ #define MSWAP(a,b) do {celt_glog tmp = a;a=b;b=tmp;} while(0)
931
+ static celt_glog median_of_5(const celt_glog *x)
932
+ {
933
+ celt_glog t0, t1, t2, t3, t4;
934
+ t2 = x[2];
935
+ if (x[0] > x[1])
936
+ {
937
+ t0 = x[1];
938
+ t1 = x[0];
939
+ } else {
940
+ t0 = x[0];
941
+ t1 = x[1];
942
+ }
943
+ if (x[3] > x[4])
944
+ {
945
+ t3 = x[4];
946
+ t4 = x[3];
947
+ } else {
948
+ t3 = x[3];
949
+ t4 = x[4];
950
+ }
951
+ if (t0 > t3)
952
+ {
953
+ MSWAP(t0, t3);
954
+ MSWAP(t1, t4);
955
+ }
956
+ if (t2 > t1)
957
+ {
958
+ if (t1 < t3)
959
+ return MING(t2, t3);
960
+ else
961
+ return MING(t4, t1);
962
+ } else {
963
+ if (t2 < t3)
964
+ return MING(t1, t3);
965
+ else
966
+ return MING(t2, t4);
967
+ }
968
+ }
969
+
970
+ static celt_glog median_of_3(const celt_glog *x)
971
+ {
972
+ celt_glog t0, t1, t2;
973
+ if (x[0] > x[1])
974
+ {
975
+ t0 = x[1];
976
+ t1 = x[0];
977
+ } else {
978
+ t0 = x[0];
979
+ t1 = x[1];
980
+ }
981
+ t2 = x[2];
982
+ if (t1 < t2)
983
+ return t1;
984
+ else if (t0 < t2)
985
+ return t2;
986
+ else
987
+ return t0;
988
+ }
989
+
990
+ static celt_glog dynalloc_analysis(const celt_glog *bandLogE, const celt_glog *bandLogE2, const celt_glog *oldBandE,
991
+ int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
992
+ int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
993
+ int effectiveBytes, opus_int32 *tot_boost_, int lfe, celt_glog *surround_dynalloc,
994
+ AnalysisInfo *analysis, int *importance, int *spread_weight, opus_val16 tone_freq, opus_val32 toneishness)
995
+ {
996
+ int i, c;
997
+ opus_int32 tot_boost=0;
998
+ celt_glog maxDepth;
999
+ VARDECL(celt_glog, follower);
1000
+ VARDECL(celt_glog, noise_floor);
1001
+ VARDECL(celt_glog, bandLogE3);
1002
+ SAVE_STACK;
1003
+ ALLOC(follower, C*nbEBands, celt_glog);
1004
+ ALLOC(noise_floor, C*nbEBands, celt_glog);
1005
+ ALLOC(bandLogE3, nbEBands, celt_glog);
1006
+ OPUS_CLEAR(offsets, nbEBands);
1007
+ /* Dynamic allocation code */
1008
+ maxDepth=-GCONST(31.9f);
1009
+ for (i=0;i<end;i++)
1010
+ {
1011
+ /* Noise floor must take into account eMeans, the depth, the width of the bands
1012
+ and the preemphasis filter (approx. square of bark band ID) */
1013
+ noise_floor[i] = GCONST(0.0625f)*logN[i]
1014
+ +GCONST(.5f)+SHL32(9-lsb_depth,DB_SHIFT)-SHL32(eMeans[i],DB_SHIFT-4)
1015
+ +GCONST(.0062f)*(i+5)*(i+5);
1016
+ }
1017
+ c=0;do
1018
+ {
1019
+ for (i=0;i<end;i++)
1020
+ maxDepth = MAXG(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
1021
+ } while (++c<C);
1022
+ {
1023
+ /* Compute a really simple masking model to avoid taking into account completely masked
1024
+ bands when computing the spreading decision. */
1025
+ VARDECL(celt_glog, mask);
1026
+ VARDECL(celt_glog, sig);
1027
+ ALLOC(mask, nbEBands, celt_glog);
1028
+ ALLOC(sig, nbEBands, celt_glog);
1029
+ for (i=0;i<end;i++)
1030
+ mask[i] = bandLogE[i]-noise_floor[i];
1031
+ if (C==2)
1032
+ {
1033
+ for (i=0;i<end;i++)
1034
+ mask[i] = MAXG(mask[i], bandLogE[nbEBands+i]-noise_floor[i]);
1035
+ }
1036
+ OPUS_COPY(sig, mask, end);
1037
+ for (i=1;i<end;i++)
1038
+ mask[i] = MAXG(mask[i], mask[i-1] - GCONST(2.f));
1039
+ for (i=end-2;i>=0;i--)
1040
+ mask[i] = MAXG(mask[i], mask[i+1] - GCONST(3.f));
1041
+ for (i=0;i<end;i++)
1042
+ {
1043
+ /* Compute SMR: Mask is never more than 72 dB below the peak and never below the noise floor.*/
1044
+ celt_glog smr = sig[i]-MAXG(MAXG(0, maxDepth-GCONST(12.f)), mask[i]);
1045
+ /* Clamp SMR to make sure we're not shifting by something negative or too large. */
1046
+ #ifdef FIXED_POINT
1047
+ /* FIXME: Use PSHR16() instead */
1048
+ int shift = -PSHR32(MAXG(-GCONST(5.f), MING(0, smr)), DB_SHIFT);
1049
+ #else
1050
+ int shift = IMIN(5, IMAX(0, -(int)floor(.5f + smr)));
1051
+ #endif
1052
+ spread_weight[i] = 32 >> shift;
1053
+ }
1054
+ /*for (i=0;i<end;i++)
1055
+ printf("%d ", spread_weight[i]);
1056
+ printf("\n");*/
1057
+ }
1058
+ /* Make sure that dynamic allocation can't make us bust the budget.
1059
+ We enable the feature starting at 24 kb/s for 20-ms frames
1060
+ and 96 kb/s for 2.5 ms frames. */
1061
+ if (effectiveBytes >= (30 + 5*LM) && !lfe)
1062
+ {
1063
+ int last=0;
1064
+ c=0;do
1065
+ {
1066
+ celt_glog offset;
1067
+ celt_glog tmp;
1068
+ celt_glog *f;
1069
+ OPUS_COPY(bandLogE3, &bandLogE2[c*nbEBands], end);
1070
+ if (LM==0) {
1071
+ /* For 2.5 ms frames, the first 8 bands have just one bin, so the
1072
+ energy is highly unreliable (high variance). For that reason,
1073
+ we take the max with the previous energy so that at least 2 bins
1074
+ are getting used. */
1075
+ for (i=0;i<IMIN(8,end);i++) bandLogE3[i] = MAXG(bandLogE2[c*nbEBands+i], oldBandE[c*nbEBands+i]);
1076
+ }
1077
+ f = &follower[c*nbEBands];
1078
+ f[0] = bandLogE3[0];
1079
+ for (i=1;i<end;i++)
1080
+ {
1081
+ /* The last band to be at least 3 dB higher than the previous one
1082
+ is the last we'll consider. Otherwise, we run into problems on
1083
+ bandlimited signals. */
1084
+ if (bandLogE3[i] > bandLogE3[i-1]+GCONST(.5f))
1085
+ last=i;
1086
+ f[i] = MING(f[i-1]+GCONST(1.5f), bandLogE3[i]);
1087
+ }
1088
+ for (i=last-1;i>=0;i--)
1089
+ f[i] = MING(f[i], MING(f[i+1]+GCONST(2.f), bandLogE3[i]));
1090
+
1091
+ /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
1092
+ The "offset" value controls how conservative we are -- a higher offset
1093
+ reduces the impact of the median filter and makes dynalloc use more bits. */
1094
+ offset = GCONST(1.f);
1095
+ for (i=2;i<end-2;i++)
1096
+ f[i] = MAXG(f[i], median_of_5(&bandLogE3[i-2])-offset);
1097
+ tmp = median_of_3(&bandLogE3[0])-offset;
1098
+ f[0] = MAXG(f[0], tmp);
1099
+ f[1] = MAXG(f[1], tmp);
1100
+ tmp = median_of_3(&bandLogE3[end-3])-offset;
1101
+ f[end-2] = MAXG(f[end-2], tmp);
1102
+ f[end-1] = MAXG(f[end-1], tmp);
1103
+
1104
+ for (i=0;i<end;i++)
1105
+ f[i] = MAXG(f[i], noise_floor[i]);
1106
+ } while (++c<C);
1107
+ if (C==2)
1108
+ {
1109
+ for (i=start;i<end;i++)
1110
+ {
1111
+ /* Consider 24 dB "cross-talk" */
1112
+ follower[nbEBands+i] = MAXG(follower[nbEBands+i], follower[ i]-GCONST(4.f));
1113
+ follower[ i] = MAXG(follower[ i], follower[nbEBands+i]-GCONST(4.f));
1114
+ follower[i] = HALF32(MAXG(0, bandLogE[i]-follower[i]) + MAXG(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
1115
+ }
1116
+ } else {
1117
+ for (i=start;i<end;i++)
1118
+ {
1119
+ follower[i] = MAXG(0, bandLogE[i]-follower[i]);
1120
+ }
1121
+ }
1122
+ for (i=start;i<end;i++)
1123
+ follower[i] = MAXG(follower[i], surround_dynalloc[i]);
1124
+ for (i=start;i<end;i++)
1125
+ {
1126
+ #ifdef FIXED_POINT
1127
+ importance[i] = PSHR32(13*celt_exp2_db(MING(follower[i], GCONST(4.f))), 16);
1128
+ #else
1129
+ importance[i] = (int)floor(.5f+13*celt_exp2_db(MING(follower[i], GCONST(4.f))));
1130
+ #endif
1131
+ }
1132
+ /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1133
+ if ((!vbr || constrained_vbr)&&!isTransient)
1134
+ {
1135
+ for (i=start;i<end;i++)
1136
+ follower[i] = HALF32(follower[i]);
1137
+ }
1138
+ for (i=start;i<end;i++)
1139
+ {
1140
+ if (i<8)
1141
+ follower[i] *= 2;
1142
+ if (i>=12)
1143
+ follower[i] = HALF32(follower[i]);
1144
+ }
1145
+ /* Compensate for Opus' under-allocation on tones. */
1146
+ if (toneishness > QCONST32(.98f, 29)) {
1147
+ #ifdef FIXED_POINT
1148
+ int freq_bin = PSHR32(MULT16_16(tone_freq, QCONST16(120/M_PI, 9)), 13+9);
1149
+ #else
1150
+ int freq_bin = (int)floor(.5 + tone_freq*120/M_PI);
1151
+ #endif
1152
+ for (i=start;i<end;i++) {
1153
+ if (freq_bin >= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += GCONST(2.f);
1154
+ if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += GCONST(1.f);
1155
+ if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += GCONST(1.f);
1156
+ if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += GCONST(.5f);
1157
+ }
1158
+ }
1159
+ #ifdef DISABLE_FLOAT_API
1160
+ (void)analysis;
1161
+ #else
1162
+ if (analysis->valid)
1163
+ {
1164
+ for (i=start;i<IMIN(LEAK_BANDS, end);i++)
1165
+ follower[i] = follower[i] + GCONST(1.f/64.f)*analysis->leak_boost[i];
1166
+ }
1167
+ #endif
1168
+ for (i=start;i<end;i++)
1169
+ {
1170
+ int width;
1171
+ int boost;
1172
+ int boost_bits;
1173
+
1174
+ follower[i] = MING(follower[i], GCONST(4));
1175
+
1176
+ follower[i] = SHR32(follower[i], 8);
1177
+ width = C*(eBands[i+1]-eBands[i])<<LM;
1178
+ if (width<6)
1179
+ {
1180
+ boost = (int)SHR32(follower[i],DB_SHIFT-8);
1181
+ boost_bits = boost*width<<BITRES;
1182
+ } else if (width > 48) {
1183
+ boost = (int)SHR32(follower[i]*8,DB_SHIFT-8);
1184
+ boost_bits = (boost*width<<BITRES)/8;
1185
+ } else {
1186
+ boost = (int)SHR32(follower[i]*width/6,DB_SHIFT-8);
1187
+ boost_bits = boost*6<<BITRES;
1188
+ }
1189
+ /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
1190
+ if ((!vbr || (constrained_vbr&&!isTransient))
1191
+ && (tot_boost+boost_bits)>>BITRES>>3 > 2*effectiveBytes/3)
1192
+ {
1193
+ opus_int32 cap = ((2*effectiveBytes/3)<<BITRES<<3);
1194
+ offsets[i] = cap-tot_boost;
1195
+ tot_boost = cap;
1196
+ break;
1197
+ } else {
1198
+ offsets[i] = boost;
1199
+ tot_boost += boost_bits;
1200
+ }
1201
+ }
1202
+ } else {
1203
+ for (i=start;i<end;i++)
1204
+ importance[i] = 13;
1205
+ }
1206
+ *tot_boost_ = tot_boost;
1207
+ RESTORE_STACK;
1208
+ return maxDepth;
1209
+ }
1210
+
1211
+ #ifdef FIXED_POINT
1212
+ void normalize_tone_input(opus_val16 *x, int len) {
1213
+ opus_val32 ac0=len;
1214
+ int i;
1215
+ int shift;
1216
+ for (i=0;i<len;i++) {
1217
+ ac0 = ADD32(ac0, SHR32(MULT16_16(x[i], x[i]), 10));
1218
+ }
1219
+ shift = 5 - (28-celt_ilog2(ac0))/2;
1220
+ if (shift > 0) {
1221
+ for (i=0;i<len;i++) {
1222
+ x[i] = PSHR32(x[i], shift);
1223
+ }
1224
+ }
1225
+ }
1226
+ int acos_approx(opus_val32 x) {
1227
+ opus_val16 x14;
1228
+ opus_val32 tmp;
1229
+ int flip = x<0;
1230
+ x = abs(x);
1231
+ x14 = x>>15;
1232
+ tmp = (762*x14>>14)-3308;
1233
+ tmp = (tmp*x14>>14)+25726;
1234
+ tmp = tmp*celt_sqrt(IMAX(0, (1<<30) - (x<<1)))>>16;
1235
+ if (flip) tmp = 25736 - tmp;
1236
+ return tmp;
1237
+ }
1238
+ #endif
1239
+
1240
+ /* Compute the LPC coefficients using a least-squares fit for both forward and backward prediction. */
1241
+ static int tone_lpc(const opus_val16 *x, int len, int delay, opus_val32 *lpc) {
1242
+ int i;
1243
+ opus_val32 r00=0, r01=0, r11=0, r02=0, r12=0, r22=0;
1244
+ opus_val32 edges;
1245
+ opus_val32 num0, num1, den;
1246
+ celt_assert(len > 2*delay);
1247
+ /* Compute correlations as if using the forward prediction covariance method. */
1248
+ for (i=0;i<len-2*delay;i++) {
1249
+ r00 += MULT16_16(x[i],x[i]);
1250
+ r01 += MULT16_16(x[i],x[i+delay]);
1251
+ r02 += MULT16_16(x[i],x[i+2*delay]);
1252
+ }
1253
+ edges = 0;
1254
+ for (i=0;i<delay;i++) edges += MULT16_16(x[len+i-2*delay],x[len+i-2*delay]) - MULT16_16(x[i],x[i]);
1255
+ r11 = r00+edges;
1256
+ edges = 0;
1257
+ for (i=0;i<delay;i++) edges += MULT16_16(x[len+i-delay],x[len+i-delay]) - MULT16_16(x[i+delay],x[i+delay]);
1258
+ r22 = r11+edges;
1259
+ edges = 0;
1260
+ for (i=0;i<delay;i++) edges += MULT16_16(x[len+i-2*delay],x[len+i-delay]) - MULT16_16(x[i],x[i+delay]);
1261
+ r12 = r01+edges;
1262
+ /* Reverse and sum to get the backward contribution. */
1263
+ {
1264
+ opus_val32 R00, R01, R11, R02, R12, R22;
1265
+ R00 = r00 + r22;
1266
+ R01 = r01 + r12;
1267
+ R11 = 2*r11;
1268
+ R02 = 2*r02;
1269
+ R12 = r12 + r01;
1270
+ R22 = r00 + r22;
1271
+ r00 = R00;
1272
+ r01 = R01;
1273
+ r11 = R11;
1274
+ r02 = R02;
1275
+ r12 = R12;
1276
+ r22 = R22;
1277
+ }
1278
+ /* Solve A*x=b, where A=[r00, r01; r01, r11] and b=[r02; r12]. */
1279
+ den = MULT32_32_Q31(r00,r11) - MULT32_32_Q31(r01,r01);
1280
+ #ifdef FIXED_POINT
1281
+ if (den <= SHR32(MULT32_32_Q31(r00,r11), 10)) return 1;
1282
+ #else
1283
+ if (den < .001f*MULT32_32_Q31(r00,r11)) return 1;
1284
+ #endif
1285
+ num1 = MULT32_32_Q31(r02,r11) - MULT32_32_Q31(r01,r12);
1286
+ if (num1 >= den) lpc[1] = QCONST32(1.f, 29);
1287
+ else if (num1 <= -den) lpc[1] = -QCONST32(1.f, 29);
1288
+ else lpc[1] = frac_div32_q29(num1, den);
1289
+ num0 = MULT32_32_Q31(r00,r12) - MULT32_32_Q31(r02,r01);
1290
+ if (HALF32(num0) >= den) lpc[0] = QCONST32(1.999999f, 29);
1291
+ else if (HALF32(num0) <= -den) lpc[0] = -QCONST32(1.999999f, 29);
1292
+ else lpc[0] = frac_div32_q29(num0, den);
1293
+ /*printf("%f %f\n", lpc[0], lpc[1]);*/
1294
+ return 0;
1295
+ }
1296
+
1297
+ /* Detects pure of nearly pure tones so we can prevent them from causing problems with the encoder. */
1298
+ static opus_val16 tone_detect(const celt_sig *in, const celt_sig *prefilter_mem, int CC, int N, int overlap, opus_val32 *toneishness, opus_int32 Fs) {
1299
+ int i;
1300
+ int delay = 1;
1301
+ int fail;
1302
+ opus_val32 lpc[2];
1303
+ opus_val16 freq;
1304
+ VARDECL(opus_val16, x);
1305
+ ALLOC(x, N+overlap, opus_val16);
1306
+ /* Shift by SIG_SHIFT+1 (+2 for stereo) to account for HF gain of the preemphasis filter. */
1307
+ if (CC==2) {
1308
+ for (i=0;i<N;i++) x[i+overlap] = PSHR32(ADD32(in[i], in[i+N+overlap]), SIG_SHIFT+2);
1309
+ for (i=0;i<overlap;i++) x[i] = PSHR32(ADD32(prefilter_mem[COMBFILTER_MAXPERIOD-overlap+i], prefilter_mem[2*COMBFILTER_MAXPERIOD-overlap+i]), SIG_SHIFT+2);
1310
+ } else {
1311
+ for (i=0;i<N;i++) x[i+overlap] = PSHR32(in[i], SIG_SHIFT+1);
1312
+ for (i=0;i<overlap;i++) x[i] = PSHR32(prefilter_mem[COMBFILTER_MAXPERIOD-overlap+i], SIG_SHIFT+1);
1313
+ }
1314
+ #ifdef FIXED_POINT
1315
+ normalize_tone_input(x, N+overlap);
1316
+ #endif
1317
+ fail = tone_lpc(x, N+overlap, delay, lpc);
1318
+ /* If our LPC filter resonates too close to DC, retry the analysis with down-sampling. */
1319
+ while (delay <= Fs/3000 && (fail || (lpc[0] > QCONST32(1.f, 29) && lpc[1] < 0))) {
1320
+ delay *= 2;
1321
+ fail = tone_lpc(x, N+overlap, delay, lpc);
1322
+ }
1323
+ /* Check that our filter has complex roots. */
1324
+ if (!fail && MULT32_32_Q31(lpc[0],lpc[0]) + MULT32_32_Q31(QCONST32(3.999999, 29), lpc[1]) < 0) {
1325
+ /* Squared radius of the poles. */
1326
+ *toneishness = -lpc[1];
1327
+ #ifdef FIXED_POINT
1328
+ freq = acos_approx(lpc[0]>>1)/delay;
1329
+ #else
1330
+ freq = acos(.5f*lpc[0])/delay;
1331
+ #endif
1332
+ } else {
1333
+ freq = -1;
1334
+ *toneishness=0;
1335
+ }
1336
+ /*printf("%f %f %f %f\n", freq, lpc[0], lpc[1], *toneishness);*/
1337
+ return freq;
1338
+ }
1339
+
1340
+ static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
1341
+ int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int nbAvailableBytes, AnalysisInfo *analysis, opus_val16 tone_freq, opus_val32 toneishness)
1342
+ {
1343
+ int c;
1344
+ VARDECL(celt_sig, _pre);
1345
+ celt_sig *pre[2];
1346
+ const CELTMode *mode;
1347
+ int pitch_index;
1348
+ opus_val16 gain1;
1349
+ opus_val16 pf_threshold;
1350
+ int pf_on;
1351
+ int qg;
1352
+ int overlap;
1353
+ SAVE_STACK;
1354
+
1355
+ mode = st->mode;
1356
+ overlap = mode->overlap;
1357
+ ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1358
+
1359
+ pre[0] = _pre;
1360
+ pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1361
+
1362
+
1363
+ c=0; do {
1364
+ OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1365
+ OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+overlap)+overlap, N);
1366
+ } while (++c<CC);
1367
+
1368
+ if (enabled)
1369
+ {
1370
+ VARDECL(opus_val16, pitch_buf);
1371
+ ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1372
+
1373
+ pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch);
1374
+ /* Don't search for the fir last 1.5 octave of the range because
1375
+ there's too many false-positives due to short-term correlation */
1376
+ pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1377
+ COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index,
1378
+ st->arch);
1379
+ pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1380
+
1381
+ gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1382
+ N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
1383
+ if (pitch_index > COMBFILTER_MAXPERIOD-2)
1384
+ pitch_index = COMBFILTER_MAXPERIOD-2;
1385
+ gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1386
+ /* If we detect that the signal is dominated by a single tone, don't rely on the standard pitch
1387
+ estimator, as it can become unreliable. */
1388
+ if (toneishness > QCONST32(.99f, 29)) {
1389
+ /* If the pitch is too high for our post-filter, apply pitch doubling until
1390
+ we can get something that fits (not ideal, but better than nothing). */
1391
+ while (tone_freq >= QCONST16(0.39f, 13)) tone_freq/=2;
1392
+ if (tone_freq > QCONST16(0.006148f, 13)) {
1393
+ #ifdef FIXED_POINT
1394
+ pitch_index = IMIN(51472/tone_freq, COMBFILTER_MAXPERIOD-2);
1395
+ #else
1396
+ pitch_index = IMIN((int)floor(.5+2.f*M_PI/tone_freq), COMBFILTER_MAXPERIOD-2);
1397
+ #endif
1398
+ } else {
1399
+ /* If the pitch is too low, using a very high pitch will actually give us an improvement
1400
+ due to the DC component of the filter that will be close to our tone. Again, not ideal,
1401
+ but if we only have a single tone, it's better than nothing. */
1402
+ pitch_index = COMBFILTER_MINPERIOD;
1403
+ }
1404
+ gain1 = QCONST16(.75f, 15);
1405
+ }
1406
+ /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1407
+ if (st->loss_rate>2)
1408
+ gain1 = HALF32(gain1);
1409
+ if (st->loss_rate>4)
1410
+ gain1 = HALF32(gain1);
1411
+ if (st->loss_rate>8)
1412
+ gain1 = 0;
1413
+ } else {
1414
+ gain1 = 0;
1415
+ pitch_index = COMBFILTER_MINPERIOD;
1416
+ }
1417
+ #ifndef DISABLE_FLOAT_API
1418
+ if (analysis->valid)
1419
+ gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);
1420
+ #else
1421
+ (void)analysis;
1422
+ #endif
1423
+ /* Gain threshold for enabling the prefilter/postfilter */
1424
+ pf_threshold = QCONST16(.2f,15);
1425
+
1426
+ /* Adjusting the threshold based on rate and continuity */
1427
+ if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1428
+ pf_threshold += QCONST16(.2f,15);
1429
+ if (nbAvailableBytes<25)
1430
+ pf_threshold += QCONST16(.1f,15);
1431
+ if (nbAvailableBytes<35)
1432
+ pf_threshold += QCONST16(.1f,15);
1433
+ if (st->prefilter_gain > QCONST16(.4f,15))
1434
+ pf_threshold -= QCONST16(.1f,15);
1435
+ if (st->prefilter_gain > QCONST16(.55f,15))
1436
+ pf_threshold -= QCONST16(.1f,15);
1437
+
1438
+ /* Hard threshold at 0.2 */
1439
+ pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1440
+ if (gain1<pf_threshold)
1441
+ {
1442
+ gain1 = 0;
1443
+ pf_on = 0;
1444
+ qg = 0;
1445
+ } else {
1446
+ /*This block is not gated by a total bits check only because
1447
+ of the nbAvailableBytes check above.*/
1448
+ if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1449
+ gain1=st->prefilter_gain;
1450
+
1451
+ #ifdef FIXED_POINT
1452
+ qg = ((gain1+1536)>>10)/3-1;
1453
+ #else
1454
+ qg = (int)floor(.5f+gain1*32/3)-1;
1455
+ #endif
1456
+ qg = IMAX(0, IMIN(7, qg));
1457
+ gain1 = QCONST16(0.09375f,15)*(qg+1);
1458
+ pf_on = 1;
1459
+ }
1460
+ /*printf("%d %f\n", pitch_index, gain1);*/
1461
+
1462
+ c=0; do {
1463
+ int offset = mode->shortMdctSize-overlap;
1464
+ st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1465
+ OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
1466
+ if (offset)
1467
+ comb_filter(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD,
1468
+ st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1469
+ st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
1470
+
1471
+ comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1472
+ st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1473
+ st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1474
+ OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
1475
+
1476
+ if (N>COMBFILTER_MAXPERIOD)
1477
+ {
1478
+ OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1479
+ } else {
1480
+ OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1481
+ OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1482
+ }
1483
+ } while (++c<CC);
1484
+
1485
+ RESTORE_STACK;
1486
+ *gain = gain1;
1487
+ *pitch = pitch_index;
1488
+ *qgain = qg;
1489
+ return pf_on;
1490
+ }
1491
+
1492
+ static int compute_vbr(const CELTMode *mode, AnalysisInfo *analysis, opus_int32 base_target,
1493
+ int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity,
1494
+ int constrained_vbr, opus_val16 stereo_saving, int tot_boost,
1495
+ opus_val16 tf_estimate, int pitch_change, celt_glog maxDepth,
1496
+ int lfe, int has_surround_mask, celt_glog surround_masking,
1497
+ celt_glog temporal_vbr)
1498
+ {
1499
+ /* The target rate in 8th bits per frame */
1500
+ opus_int32 target;
1501
+ int coded_bins;
1502
+ int coded_bands;
1503
+ opus_val16 tf_calibration;
1504
+ int nbEBands;
1505
+ const opus_int16 *eBands;
1506
+
1507
+ nbEBands = mode->nbEBands;
1508
+ eBands = mode->eBands;
1509
+
1510
+ coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1511
+ coded_bins = eBands[coded_bands]<<LM;
1512
+ if (C==2)
1513
+ coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
1514
+
1515
+ target = base_target;
1516
+
1517
+ /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1518
+ #ifndef DISABLE_FLOAT_API
1519
+ if (analysis->valid && analysis->activity<.4)
1520
+ target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
1521
+ #endif
1522
+ /* Stereo savings */
1523
+ if (C==2)
1524
+ {
1525
+ int coded_stereo_bands;
1526
+ int coded_stereo_dof;
1527
+ opus_val16 max_frac;
1528
+ coded_stereo_bands = IMIN(intensity, coded_bands);
1529
+ coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1530
+ /* Maximum fraction of the bits we can save if the signal is mono. */
1531
+ max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
1532
+ stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
1533
+ /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1534
+ target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1535
+ SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
1536
+ }
1537
+ /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
1538
+ target += tot_boost-(19<<LM);
1539
+ /* Apply transient boost, compensating for average boost. */
1540
+ tf_calibration = QCONST16(0.044f,14);
1541
+ target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
1542
+
1543
+ #ifndef DISABLE_FLOAT_API
1544
+ /* Apply tonality boost */
1545
+ if (analysis->valid && !lfe)
1546
+ {
1547
+ opus_int32 tonal_target;
1548
+ float tonal;
1549
+
1550
+ /* Tonality boost (compensating for the average). */
1551
+ tonal = MAX16(0.f,analysis->tonality-.15f)-0.12f;
1552
+ tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1553
+ if (pitch_change)
1554
+ tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f);
1555
+ /*printf("%f %f ", analysis->tonality, tonal);*/
1556
+ target = tonal_target;
1557
+ }
1558
+ #else
1559
+ (void)analysis;
1560
+ (void)pitch_change;
1561
+ #endif
1562
+
1563
+ if (has_surround_mask&&!lfe)
1564
+ {
1565
+ opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(SHR32(surround_masking,DB_SHIFT-10),coded_bins<<BITRES), 10);
1566
+ /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1567
+ target = IMAX(target/4, surround_target);
1568
+ }
1569
+
1570
+ {
1571
+ opus_int32 floor_depth;
1572
+ int bins;
1573
+ bins = eBands[nbEBands-2]<<LM;
1574
+ /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1575
+ floor_depth = (opus_int32)SHR32(MULT16_32_Q15((C*bins<<BITRES),maxDepth), DB_SHIFT-15);
1576
+ floor_depth = IMAX(floor_depth, target>>2);
1577
+ target = IMIN(target, floor_depth);
1578
+ /*printf("%f %d\n", maxDepth, floor_depth);*/
1579
+ }
1580
+
1581
+ /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
1582
+ for long. Needs tuning. */
1583
+ if ((!has_surround_mask||lfe) && constrained_vbr)
1584
+ {
1585
+ target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target);
1586
+ }
1587
+
1588
+ if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
1589
+ {
1590
+ opus_val16 amount;
1591
+ opus_val16 tvbr_factor;
1592
+ amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
1593
+ tvbr_factor = SHR32(MULT16_16(SHR32(temporal_vbr, DB_SHIFT-10), amount), 10);
1594
+ target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1595
+ }
1596
+
1597
+ /* Don't allow more than doubling the rate */
1598
+ target = IMIN(2*base_target, target);
1599
+
1600
+ return target;
1601
+ }
1602
+
1603
+ int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1604
+ {
1605
+ int i, c, N;
1606
+ opus_int32 bits;
1607
+ ec_enc _enc;
1608
+ VARDECL(celt_sig, in);
1609
+ VARDECL(celt_sig, freq);
1610
+ VARDECL(celt_norm, X);
1611
+ VARDECL(celt_ener, bandE);
1612
+ VARDECL(celt_glog, bandLogE);
1613
+ VARDECL(celt_glog, bandLogE2);
1614
+ VARDECL(int, fine_quant);
1615
+ VARDECL(celt_glog, error);
1616
+ VARDECL(int, pulses);
1617
+ VARDECL(int, cap);
1618
+ VARDECL(int, offsets);
1619
+ VARDECL(int, importance);
1620
+ VARDECL(int, spread_weight);
1621
+ VARDECL(int, fine_priority);
1622
+ VARDECL(int, tf_res);
1623
+ VARDECL(unsigned char, collapse_masks);
1624
+ celt_sig *prefilter_mem;
1625
+ celt_glog *oldBandE, *oldLogE, *oldLogE2, *energyError;
1626
+ int shortBlocks=0;
1627
+ int isTransient=0;
1628
+ const int CC = st->channels;
1629
+ const int C = st->stream_channels;
1630
+ int LM, M;
1631
+ int tf_select;
1632
+ int nbFilledBytes, nbAvailableBytes;
1633
+ int start;
1634
+ int end;
1635
+ int effEnd;
1636
+ int codedBands;
1637
+ int alloc_trim;
1638
+ int pitch_index=COMBFILTER_MINPERIOD;
1639
+ opus_val16 gain1 = 0;
1640
+ int dual_stereo=0;
1641
+ int effectiveBytes;
1642
+ int dynalloc_logp;
1643
+ opus_int32 vbr_rate;
1644
+ opus_int32 total_bits;
1645
+ opus_int32 total_boost;
1646
+ opus_int32 balance;
1647
+ opus_int32 tell;
1648
+ opus_int32 tell0_frac;
1649
+ int prefilter_tapset=0;
1650
+ int pf_on;
1651
+ int anti_collapse_rsv;
1652
+ int anti_collapse_on=0;
1653
+ int silence=0;
1654
+ int tf_chan = 0;
1655
+ opus_val16 tf_estimate;
1656
+ int pitch_change=0;
1657
+ opus_int32 tot_boost;
1658
+ opus_val32 sample_max;
1659
+ celt_glog maxDepth;
1660
+ const OpusCustomMode *mode;
1661
+ int nbEBands;
1662
+ int overlap;
1663
+ const opus_int16 *eBands;
1664
+ int secondMdct;
1665
+ int signalBandwidth;
1666
+ int transient_got_disabled=0;
1667
+ celt_glog surround_masking=0;
1668
+ celt_glog temporal_vbr=0;
1669
+ celt_glog surround_trim = 0;
1670
+ opus_int32 equiv_rate;
1671
+ int hybrid;
1672
+ int weak_transient = 0;
1673
+ int enable_tf_analysis;
1674
+ opus_val16 tone_freq=-1;
1675
+ opus_val32 toneishness=0;
1676
+ VARDECL(celt_glog, surround_dynalloc);
1677
+ ALLOC_STACK;
1678
+
1679
+ mode = st->mode;
1680
+ nbEBands = mode->nbEBands;
1681
+ overlap = mode->overlap;
1682
+ eBands = mode->eBands;
1683
+ start = st->start;
1684
+ end = st->end;
1685
+ hybrid = start != 0;
1686
+ tf_estimate = 0;
1687
+ if (nbCompressedBytes<2 || pcm==NULL)
1688
+ {
1689
+ RESTORE_STACK;
1690
+ return OPUS_BAD_ARG;
1691
+ }
1692
+
1693
+ frame_size *= st->upsample;
1694
+ for (LM=0;LM<=mode->maxLM;LM++)
1695
+ if (mode->shortMdctSize<<LM==frame_size)
1696
+ break;
1697
+ if (LM>mode->maxLM)
1698
+ {
1699
+ RESTORE_STACK;
1700
+ return OPUS_BAD_ARG;
1701
+ }
1702
+ M=1<<LM;
1703
+ N = M*mode->shortMdctSize;
1704
+
1705
+ prefilter_mem = st->in_mem+CC*(overlap);
1706
+ oldBandE = (celt_glog*)(st->in_mem+CC*(overlap+COMBFILTER_MAXPERIOD));
1707
+ oldLogE = oldBandE + CC*nbEBands;
1708
+ oldLogE2 = oldLogE + CC*nbEBands;
1709
+ energyError = oldLogE2 + CC*nbEBands;
1710
+
1711
+ if (enc==NULL)
1712
+ {
1713
+ tell0_frac=tell=1;
1714
+ nbFilledBytes=0;
1715
+ } else {
1716
+ tell0_frac=ec_tell_frac(enc);
1717
+ tell=ec_tell(enc);
1718
+ nbFilledBytes=(tell+4)>>3;
1719
+ }
1720
+
1721
+ #ifdef CUSTOM_MODES
1722
+ if (st->signalling && enc==NULL)
1723
+ {
1724
+ int tmp = (mode->effEBands-end)>>1;
1725
+ end = st->end = IMAX(1, mode->effEBands-tmp);
1726
+ compressed[0] = tmp<<5;
1727
+ compressed[0] |= LM<<3;
1728
+ compressed[0] |= (C==2)<<2;
1729
+ /* Convert "standard mode" to Opus header */
1730
+ if (mode->Fs==48000 && mode->shortMdctSize==120)
1731
+ {
1732
+ int c0 = toOpus(compressed[0]);
1733
+ if (c0<0)
1734
+ {
1735
+ RESTORE_STACK;
1736
+ return OPUS_BAD_ARG;
1737
+ }
1738
+ compressed[0] = c0;
1739
+ }
1740
+ compressed++;
1741
+ nbCompressedBytes--;
1742
+ }
1743
+ #else
1744
+ celt_assert(st->signalling==0);
1745
+ #endif
1746
+
1747
+ /* Can't produce more than 1275 output bytes */
1748
+ nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1749
+
1750
+ if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1751
+ {
1752
+ opus_int32 den=mode->Fs>>BITRES;
1753
+ vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1754
+ #ifdef CUSTOM_MODES
1755
+ if (st->signalling)
1756
+ vbr_rate -= 8<<BITRES;
1757
+ #endif
1758
+ effectiveBytes = vbr_rate>>(3+BITRES);
1759
+ } else {
1760
+ opus_int32 tmp;
1761
+ vbr_rate = 0;
1762
+ tmp = st->bitrate*frame_size;
1763
+ if (tell>1)
1764
+ tmp += tell*mode->Fs;
1765
+ if (st->bitrate!=OPUS_BITRATE_MAX)
1766
+ {
1767
+ nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1768
+ (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1769
+ if (enc != NULL)
1770
+ ec_enc_shrink(enc, nbCompressedBytes);
1771
+ }
1772
+ effectiveBytes = nbCompressedBytes - nbFilledBytes;
1773
+ }
1774
+ nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1775
+ equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
1776
+ if (st->bitrate != OPUS_BITRATE_MAX)
1777
+ equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
1778
+
1779
+ if (enc==NULL)
1780
+ {
1781
+ ec_enc_init(&_enc, compressed, nbCompressedBytes);
1782
+ enc = &_enc;
1783
+ }
1784
+
1785
+ if (vbr_rate>0)
1786
+ {
1787
+ /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1788
+ target rate and buffering.
1789
+ We must do this up front so that bust-prevention logic triggers
1790
+ correctly if we don't have enough bits. */
1791
+ if (st->constrained_vbr)
1792
+ {
1793
+ opus_int32 vbr_bound;
1794
+ opus_int32 max_allowed;
1795
+ /* We could use any multiple of vbr_rate as bound (depending on the
1796
+ delay).
1797
+ This is clamped to ensure we use at least two bytes if the encoder
1798
+ was entirely empty, but to allow 0 in hybrid mode. */
1799
+ vbr_bound = vbr_rate;
1800
+ max_allowed = IMIN(IMAX(tell==1?2:0,
1801
+ (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1802
+ nbAvailableBytes);
1803
+ if(max_allowed < nbAvailableBytes)
1804
+ {
1805
+ nbCompressedBytes = nbFilledBytes+max_allowed;
1806
+ nbAvailableBytes = max_allowed;
1807
+ ec_enc_shrink(enc, nbCompressedBytes);
1808
+ }
1809
+ }
1810
+ }
1811
+ total_bits = nbCompressedBytes*8;
1812
+
1813
+ effEnd = end;
1814
+ if (effEnd > mode->effEBands)
1815
+ effEnd = mode->effEBands;
1816
+
1817
+ ALLOC(in, CC*(N+overlap), celt_sig);
1818
+
1819
+ sample_max=MAX32(st->overlap_max, celt_maxabs_res(pcm, C*(N-overlap)/st->upsample));
1820
+ st->overlap_max=celt_maxabs_res(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1821
+ sample_max=MAX32(sample_max, st->overlap_max);
1822
+ #ifdef FIXED_POINT
1823
+ silence = (sample_max==0);
1824
+ #else
1825
+ silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1826
+ #endif
1827
+ #ifdef FUZZING
1828
+ if ((rand()&0x3F)==0)
1829
+ silence = 1;
1830
+ #endif
1831
+ if (tell==1)
1832
+ ec_enc_bit_logp(enc, silence, 15);
1833
+ else
1834
+ silence=0;
1835
+ if (silence)
1836
+ {
1837
+ /*In VBR mode there is no need to send more than the minimum. */
1838
+ if (vbr_rate>0)
1839
+ {
1840
+ effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1841
+ total_bits=nbCompressedBytes*8;
1842
+ nbAvailableBytes=2;
1843
+ ec_enc_shrink(enc, nbCompressedBytes);
1844
+ }
1845
+ /* Pretend we've filled all the remaining bits with zeros
1846
+ (that's what the initialiser did anyway) */
1847
+ tell = nbCompressedBytes*8;
1848
+ enc->nbits_total+=tell-ec_tell(enc);
1849
+ }
1850
+ c=0; do {
1851
+ int need_clip=0;
1852
+ #ifndef FIXED_POINT
1853
+ need_clip = st->clip && sample_max>65536.f;
1854
+ #endif
1855
+ celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
1856
+ mode->preemph, st->preemph_memE+c, need_clip);
1857
+ } while (++c<CC);
1858
+
1859
+
1860
+ tone_freq = tone_detect(in+overlap, prefilter_mem, 1, N, overlap, &toneishness, mode->Fs);
1861
+ /* Find pitch period and gain */
1862
+ {
1863
+ int enabled;
1864
+ int qg;
1865
+ enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf
1866
+ && st->complexity >= 5;
1867
+
1868
+ prefilter_tapset = st->tapset_decision;
1869
+ pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, nbAvailableBytes, &st->analysis, tone_freq, toneishness);
1870
+ if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
1871
+ && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
1872
+ pitch_change = 1;
1873
+ if (pf_on==0)
1874
+ {
1875
+ if(!hybrid && tell+16<=total_bits)
1876
+ ec_enc_bit_logp(enc, 0, 1);
1877
+ } else {
1878
+ /*This block is not gated by a total bits check only because
1879
+ of the nbAvailableBytes check above.*/
1880
+ int octave;
1881
+ ec_enc_bit_logp(enc, 1, 1);
1882
+ pitch_index += 1;
1883
+ octave = EC_ILOG(pitch_index)-5;
1884
+ ec_enc_uint(enc, octave, 6);
1885
+ ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1886
+ pitch_index -= 1;
1887
+ ec_enc_bits(enc, qg, 3);
1888
+ ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1889
+ }
1890
+ }
1891
+
1892
+ isTransient = 0;
1893
+ shortBlocks = 0;
1894
+ if (st->complexity >= 1 && !st->lfe)
1895
+ {
1896
+ /* Reduces the likelihood of energy instability on fricatives at low bitrate
1897
+ in hybrid mode. It seems like we still want to have real transients on vowels
1898
+ though (small SILK quantization offset value). */
1899
+ int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
1900
+ isTransient = transient_analysis(in, N+overlap, CC,
1901
+ &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient, tone_freq, toneishness);
1902
+ }
1903
+ if (LM>0 && ec_tell(enc)+3<=total_bits)
1904
+ {
1905
+ if (isTransient)
1906
+ shortBlocks = M;
1907
+ } else {
1908
+ isTransient = 0;
1909
+ transient_got_disabled=1;
1910
+ }
1911
+
1912
+ ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1913
+ ALLOC(bandE,nbEBands*CC, celt_ener);
1914
+ ALLOC(bandLogE,nbEBands*CC, celt_glog);
1915
+
1916
+ secondMdct = shortBlocks && st->complexity>=8;
1917
+ ALLOC(bandLogE2, C*nbEBands, celt_glog);
1918
+ if (secondMdct)
1919
+ {
1920
+ compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
1921
+ compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
1922
+ amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
1923
+ for (c=0;c<C;c++)
1924
+ {
1925
+ for (i=0;i<end;i++)
1926
+ bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
1927
+ }
1928
+ }
1929
+
1930
+ compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
1931
+ /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
1932
+ at the Opus layer), just abort. */
1933
+ celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N])));
1934
+ if (CC==2&&C==1)
1935
+ tf_chan = 0;
1936
+ compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
1937
+
1938
+ if (st->lfe)
1939
+ {
1940
+ for (i=2;i<end;i++)
1941
+ {
1942
+ bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
1943
+ bandE[i] = MAX32(bandE[i], EPSILON);
1944
+ }
1945
+ }
1946
+ amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
1947
+
1948
+ ALLOC(surround_dynalloc, C*nbEBands, celt_glog);
1949
+ OPUS_CLEAR(surround_dynalloc, end);
1950
+ /* This computes how much masking takes place between surround channels */
1951
+ if (!hybrid&&st->energy_mask&&!st->lfe)
1952
+ {
1953
+ int mask_end;
1954
+ int midband;
1955
+ int count_dynalloc;
1956
+ opus_val32 mask_avg=0;
1957
+ opus_val32 diff=0;
1958
+ int count=0;
1959
+ mask_end = IMAX(2,st->lastCodedBands);
1960
+ for (c=0;c<C;c++)
1961
+ {
1962
+ for(i=0;i<mask_end;i++)
1963
+ {
1964
+ celt_glog mask;
1965
+ opus_val16 mask16;
1966
+ mask = MAXG(MING(st->energy_mask[nbEBands*c+i],
1967
+ GCONST(.25f)), -GCONST(2.0f));
1968
+ if (mask > 0)
1969
+ mask = HALF32(mask);
1970
+ mask16 = SHR32(mask, DB_SHIFT-10);
1971
+ mask_avg += MULT16_16(mask16, eBands[i+1]-eBands[i]);
1972
+ count += eBands[i+1]-eBands[i];
1973
+ diff += MULT16_16(mask16, 1+2*i-mask_end);
1974
+ }
1975
+ }
1976
+ celt_assert(count>0);
1977
+ mask_avg = SHL32(DIV32_16(mask_avg,count), DB_SHIFT-10);
1978
+ mask_avg += GCONST(.2f);
1979
+ diff = SHL32(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end), DB_SHIFT-10);
1980
+ /* Again, being conservative */
1981
+ diff = HALF32(diff);
1982
+ diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f));
1983
+ /* Find the band that's in the middle of the coded spectrum */
1984
+ for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
1985
+ count_dynalloc=0;
1986
+ for(i=0;i<mask_end;i++)
1987
+ {
1988
+ opus_val32 lin;
1989
+ celt_glog unmask;
1990
+ lin = mask_avg + diff*(i-midband);
1991
+ if (C==2)
1992
+ unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i]);
1993
+ else
1994
+ unmask = st->energy_mask[i];
1995
+ unmask = MING(unmask, GCONST(.0f));
1996
+ unmask -= lin;
1997
+ if (unmask > GCONST(.25f))
1998
+ {
1999
+ surround_dynalloc[i] = unmask - GCONST(.25f);
2000
+ count_dynalloc++;
2001
+ }
2002
+ }
2003
+ if (count_dynalloc>=3)
2004
+ {
2005
+ /* If we need dynalloc in many bands, it's probably because our
2006
+ initial masking rate was too low. */
2007
+ mask_avg += GCONST(.25f);
2008
+ if (mask_avg>0)
2009
+ {
2010
+ /* Something went really wrong in the original calculations,
2011
+ disabling masking. */
2012
+ mask_avg = 0;
2013
+ diff = 0;
2014
+ OPUS_CLEAR(surround_dynalloc, mask_end);
2015
+ } else {
2016
+ for(i=0;i<mask_end;i++)
2017
+ surround_dynalloc[i] = MAXG(0, surround_dynalloc[i]-GCONST(.25f));
2018
+ }
2019
+ }
2020
+ mask_avg += GCONST(.2f);
2021
+ /* Convert to 1/64th units used for the trim */
2022
+ surround_trim = 64*diff;
2023
+ /*printf("%d %d ", mask_avg, surround_trim);*/
2024
+ surround_masking = mask_avg;
2025
+ }
2026
+ /* Temporal VBR (but not for LFE) */
2027
+ if (!st->lfe)
2028
+ {
2029
+ celt_glog follow=-QCONST32(10.0f, DB_SHIFT-5);
2030
+ opus_val32 frame_avg=0;
2031
+ celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT-5)):0;
2032
+ for(i=start;i<end;i++)
2033
+ {
2034
+ follow = MAXG(follow-QCONST32(1.0f, DB_SHIFT-5), SHR32(bandLogE[i],5)-offset);
2035
+ if (C==2)
2036
+ follow = MAXG(follow, SHR32(bandLogE[i+nbEBands],5)-offset);
2037
+ frame_avg += follow;
2038
+ }
2039
+ frame_avg /= (end-start);
2040
+ temporal_vbr = SUB32(SHL32(frame_avg, 5),st->spec_avg);
2041
+ temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr));
2042
+ st->spec_avg += MULT16_32_Q15(QCONST16(.02f, 15), temporal_vbr);
2043
+ }
2044
+ /*for (i=0;i<21;i++)
2045
+ printf("%f ", bandLogE[i]);
2046
+ printf("\n");*/
2047
+
2048
+ if (!secondMdct)
2049
+ {
2050
+ OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
2051
+ }
2052
+
2053
+ /* Last chance to catch any transient we might have missed in the
2054
+ time-domain analysis */
2055
+ if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
2056
+ {
2057
+ if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
2058
+ {
2059
+ isTransient = 1;
2060
+ shortBlocks = M;
2061
+ compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2062
+ compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2063
+ amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2064
+ /* Compensate for the scaling of short vs long mdcts */
2065
+ for (c=0;c<C;c++)
2066
+ {
2067
+ for (i=0;i<end;i++)
2068
+ bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2069
+ }
2070
+ tf_estimate = QCONST16(.2f,14);
2071
+ }
2072
+ }
2073
+
2074
+ if (LM>0 && ec_tell(enc)+3<=total_bits)
2075
+ ec_enc_bit_logp(enc, isTransient, 3);
2076
+
2077
+ ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
2078
+
2079
+ /* Band normalisation */
2080
+ normalise_bands(mode, freq, X, bandE, effEnd, C, M);
2081
+
2082
+ enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe && toneishness < QCONST32(.98f, 29);
2083
+
2084
+ ALLOC(offsets, nbEBands, int);
2085
+ ALLOC(importance, nbEBands, int);
2086
+ ALLOC(spread_weight, nbEBands, int);
2087
+
2088
+ maxDepth = dynalloc_analysis(bandLogE, bandLogE2, oldBandE, nbEBands, start, end, C, offsets,
2089
+ st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
2090
+ eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight, tone_freq, toneishness);
2091
+
2092
+ ALLOC(tf_res, nbEBands, int);
2093
+ /* Disable variable tf resolution for hybrid and at very low bitrate */
2094
+ if (enable_tf_analysis)
2095
+ {
2096
+ int lambda;
2097
+ lambda = IMAX(80, 20480/effectiveBytes + 2);
2098
+ tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
2099
+ for (i=effEnd;i<end;i++)
2100
+ tf_res[i] = tf_res[effEnd-1];
2101
+ } else if (hybrid && weak_transient)
2102
+ {
2103
+ /* For weak transients, we rely on the fact that improving time resolution using
2104
+ TF on a long window is imperfect and will not result in an energy collapse at
2105
+ low bitrate. */
2106
+ for (i=0;i<end;i++)
2107
+ tf_res[i] = 1;
2108
+ tf_select=0;
2109
+ } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
2110
+ {
2111
+ /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
2112
+ for (i=0;i<end;i++)
2113
+ tf_res[i] = 0;
2114
+ tf_select=isTransient;
2115
+ } else {
2116
+ for (i=0;i<end;i++)
2117
+ tf_res[i] = isTransient;
2118
+ tf_select=0;
2119
+ }
2120
+
2121
+ ALLOC(error, C*nbEBands, celt_glog);
2122
+ c=0;
2123
+ do {
2124
+ for (i=start;i<end;i++)
2125
+ {
2126
+ /* When the energy is stable, slightly bias energy quantization towards
2127
+ the previous error to make the gain more stable (a constant offset is
2128
+ better than fluctuations). */
2129
+ if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < GCONST(2.f))
2130
+ {
2131
+ bandLogE[i+c*nbEBands] -= MULT16_32_Q15(QCONST16(0.25f, 15), energyError[i+c*nbEBands]);
2132
+ }
2133
+ }
2134
+ } while (++c < C);
2135
+ quant_coarse_energy(mode, start, end, effEnd, bandLogE,
2136
+ oldBandE, total_bits, error, enc,
2137
+ C, LM, nbAvailableBytes, st->force_intra,
2138
+ &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2139
+
2140
+ tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
2141
+
2142
+ if (ec_tell(enc)+4<=total_bits)
2143
+ {
2144
+ if (st->lfe)
2145
+ {
2146
+ st->tapset_decision = 0;
2147
+ st->spread_decision = SPREAD_NORMAL;
2148
+ } else if (hybrid)
2149
+ {
2150
+ if (st->complexity == 0)
2151
+ st->spread_decision = SPREAD_NONE;
2152
+ else if (isTransient)
2153
+ st->spread_decision = SPREAD_NORMAL;
2154
+ else
2155
+ st->spread_decision = SPREAD_AGGRESSIVE;
2156
+ } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
2157
+ {
2158
+ if (st->complexity == 0)
2159
+ st->spread_decision = SPREAD_NONE;
2160
+ else
2161
+ st->spread_decision = SPREAD_NORMAL;
2162
+ } else {
2163
+ /* Disable new spreading+tapset estimator until we can show it works
2164
+ better than the old one. So far it seems like spreading_decision()
2165
+ works best. */
2166
+ #if 0
2167
+ if (st->analysis.valid)
2168
+ {
2169
+ static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
2170
+ static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
2171
+ static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
2172
+ static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
2173
+ st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
2174
+ st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
2175
+ } else
2176
+ #endif
2177
+ {
2178
+ st->spread_decision = spreading_decision(mode, X,
2179
+ &st->tonal_average, st->spread_decision, &st->hf_average,
2180
+ &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
2181
+ }
2182
+ /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
2183
+ /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
2184
+ }
2185
+ ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2186
+ } else {
2187
+ st->spread_decision = SPREAD_NORMAL;
2188
+ }
2189
+
2190
+ /* For LFE, everything interesting is in the first band */
2191
+ if (st->lfe)
2192
+ offsets[0] = IMIN(8, effectiveBytes/3);
2193
+ ALLOC(cap, nbEBands, int);
2194
+ init_caps(mode,cap,LM,C);
2195
+
2196
+ dynalloc_logp = 6;
2197
+ total_bits<<=BITRES;
2198
+ total_boost = 0;
2199
+ tell = ec_tell_frac(enc);
2200
+ for (i=start;i<end;i++)
2201
+ {
2202
+ int width, quanta;
2203
+ int dynalloc_loop_logp;
2204
+ int boost;
2205
+ int j;
2206
+ width = C*(eBands[i+1]-eBands[i])<<LM;
2207
+ /* quanta is 6 bits, but no more than 1 bit/sample
2208
+ and no less than 1/8 bit/sample */
2209
+ quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2210
+ dynalloc_loop_logp = dynalloc_logp;
2211
+ boost = 0;
2212
+ for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
2213
+ && boost < cap[i]; j++)
2214
+ {
2215
+ int flag;
2216
+ flag = j<offsets[i];
2217
+ ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
2218
+ tell = ec_tell_frac(enc);
2219
+ if (!flag)
2220
+ break;
2221
+ boost += quanta;
2222
+ total_boost += quanta;
2223
+ dynalloc_loop_logp = 1;
2224
+ }
2225
+ /* Making dynalloc more likely */
2226
+ if (j)
2227
+ dynalloc_logp = IMAX(2, dynalloc_logp-1);
2228
+ offsets[i] = boost;
2229
+ }
2230
+
2231
+ if (C==2)
2232
+ {
2233
+ static const opus_val16 intensity_thresholds[21]=
2234
+ /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
2235
+ { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
2236
+ static const opus_val16 intensity_histeresis[21]=
2237
+ { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8};
2238
+
2239
+ /* Always use MS for 2.5 ms frames until we can do a better analysis */
2240
+ if (LM!=0)
2241
+ dual_stereo = stereo_analysis(mode, X, LM, N);
2242
+
2243
+ st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
2244
+ intensity_thresholds, intensity_histeresis, 21, st->intensity);
2245
+ st->intensity = IMIN(end,IMAX(start, st->intensity));
2246
+ }
2247
+
2248
+ alloc_trim = 5;
2249
+ if (tell+(6<<BITRES) <= total_bits - total_boost)
2250
+ {
2251
+ if (start > 0 || st->lfe)
2252
+ {
2253
+ st->stereo_saving = 0;
2254
+ alloc_trim = 5;
2255
+ } else {
2256
+ alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
2257
+ end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
2258
+ st->intensity, surround_trim, equiv_rate, st->arch);
2259
+ }
2260
+ ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
2261
+ tell = ec_tell_frac(enc);
2262
+ }
2263
+
2264
+ /* Variable bitrate */
2265
+ if (vbr_rate>0)
2266
+ {
2267
+ opus_val16 alpha;
2268
+ opus_int32 delta;
2269
+ /* The target rate in 8th bits per frame */
2270
+ opus_int32 target, base_target;
2271
+ opus_int32 min_allowed;
2272
+ int lm_diff = mode->maxLM - LM;
2273
+
2274
+ /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
2275
+ The CELT allocator will just not be able to use more than that anyway. */
2276
+ nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
2277
+ if (!hybrid)
2278
+ {
2279
+ base_target = vbr_rate - ((40*C+20)<<BITRES);
2280
+ } else {
2281
+ base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
2282
+ }
2283
+
2284
+ if (st->constrained_vbr)
2285
+ base_target += (st->vbr_offset>>lm_diff);
2286
+
2287
+ if (!hybrid)
2288
+ {
2289
+ target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
2290
+ st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2291
+ st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
2292
+ st->lfe, st->energy_mask!=NULL, surround_masking,
2293
+ temporal_vbr);
2294
+ } else {
2295
+ target = base_target;
2296
+ /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2297
+ if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
2298
+ if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
2299
+ /* Boosting bitrate on transients and vowels with significant temporal
2300
+ spikes. */
2301
+ target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
2302
+ /* If we have a strong transient, let's make sure it has enough bits to code
2303
+ the first two bands, so that it can use folding rather than noise. */
2304
+ if (tf_estimate > QCONST16(.7f,14))
2305
+ target = IMAX(target, 50<<BITRES);
2306
+ }
2307
+ /* The current offset is removed from the target and the space used
2308
+ so far is added*/
2309
+ target=target+tell;
2310
+ /* In VBR mode the frame size must not be reduced so much that it would
2311
+ result in the encoder running out of bits.
2312
+ The margin of 2 bytes ensures that none of the bust-prevention logic
2313
+ in the decoder will have triggered so far. */
2314
+ min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
2315
+ /* Take into account the 37 bits we need to have left in the packet to
2316
+ signal a redundant frame in hybrid mode. Creating a shorter packet would
2317
+ create an entropy coder desync. */
2318
+ if (hybrid)
2319
+ min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
2320
+
2321
+ nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
2322
+ nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
2323
+ nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2324
+
2325
+ /* By how much did we "miss" the target on that frame */
2326
+ delta = target - vbr_rate;
2327
+
2328
+ target=nbAvailableBytes<<(BITRES+3);
2329
+
2330
+ /*If the frame is silent we don't adjust our drift, otherwise
2331
+ the encoder will shoot to very high rates after hitting a
2332
+ span of silence, but we do allow the bitres to refill.
2333
+ This means that we'll undershoot our target in CVBR/VBR modes
2334
+ on files with lots of silence. */
2335
+ if(silence)
2336
+ {
2337
+ nbAvailableBytes = 2;
2338
+ target = 2*8<<BITRES;
2339
+ delta = 0;
2340
+ }
2341
+
2342
+ if (st->vbr_count < 970)
2343
+ {
2344
+ st->vbr_count++;
2345
+ alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
2346
+ } else
2347
+ alpha = QCONST16(.001f,15);
2348
+ /* How many bits have we used in excess of what we're allowed */
2349
+ if (st->constrained_vbr)
2350
+ st->vbr_reservoir += target - vbr_rate;
2351
+ /*printf ("%d\n", st->vbr_reservoir);*/
2352
+
2353
+ /* Compute the offset we need to apply in order to reach the target */
2354
+ if (st->constrained_vbr)
2355
+ {
2356
+ st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
2357
+ st->vbr_offset = -st->vbr_drift;
2358
+ }
2359
+ /*printf ("%d\n", st->vbr_drift);*/
2360
+
2361
+ if (st->constrained_vbr && st->vbr_reservoir < 0)
2362
+ {
2363
+ /* We're under the min value -- increase rate */
2364
+ int adjust = (-st->vbr_reservoir)/(8<<BITRES);
2365
+ /* Unless we're just coding silence */
2366
+ nbAvailableBytes += silence?0:adjust;
2367
+ st->vbr_reservoir = 0;
2368
+ /*printf ("+%d\n", adjust);*/
2369
+ }
2370
+ nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2371
+ /*printf("%d\n", nbCompressedBytes*50*8);*/
2372
+ /* This moves the raw bits to take into account the new compressed size */
2373
+ ec_enc_shrink(enc, nbCompressedBytes);
2374
+ }
2375
+
2376
+ /* Bit allocation */
2377
+ ALLOC(fine_quant, nbEBands, int);
2378
+ ALLOC(pulses, nbEBands, int);
2379
+ ALLOC(fine_priority, nbEBands, int);
2380
+
2381
+ /* bits = packet size - where we are - safety*/
2382
+ bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2383
+ anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2384
+ bits -= anti_collapse_rsv;
2385
+ signalBandwidth = end-1;
2386
+ #ifndef DISABLE_FLOAT_API
2387
+ if (st->analysis.valid)
2388
+ {
2389
+ int min_bandwidth;
2390
+ if (equiv_rate < (opus_int32)32000*C)
2391
+ min_bandwidth = 13;
2392
+ else if (equiv_rate < (opus_int32)48000*C)
2393
+ min_bandwidth = 16;
2394
+ else if (equiv_rate < (opus_int32)60000*C)
2395
+ min_bandwidth = 18;
2396
+ else if (equiv_rate < (opus_int32)80000*C)
2397
+ min_bandwidth = 19;
2398
+ else
2399
+ min_bandwidth = 20;
2400
+ signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2401
+ }
2402
+ #endif
2403
+ if (st->lfe)
2404
+ signalBandwidth = 1;
2405
+ codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
2406
+ alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2407
+ fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2408
+ if (st->lastCodedBands)
2409
+ st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2410
+ else
2411
+ st->lastCodedBands = codedBands;
2412
+
2413
+ quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, enc, C);
2414
+
2415
+ /* Residual quantisation */
2416
+ ALLOC(collapse_masks, C*nbEBands, unsigned char);
2417
+ quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
2418
+ bandE, pulses, shortBlocks, st->spread_decision,
2419
+ dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
2420
+ balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv);
2421
+
2422
+ if (anti_collapse_rsv > 0)
2423
+ {
2424
+ anti_collapse_on = st->consec_transient<2;
2425
+ #ifdef FUZZING
2426
+ anti_collapse_on = rand()&0x1;
2427
+ #endif
2428
+ ec_enc_bits(enc, anti_collapse_on, 1);
2429
+ }
2430
+ quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2431
+ OPUS_CLEAR(energyError, nbEBands*CC);
2432
+ c=0;
2433
+ do {
2434
+ for (i=start;i<end;i++)
2435
+ {
2436
+ energyError[i+c*nbEBands] = MAXG(-GCONST(0.5f), MING(GCONST(0.5f), error[i+c*nbEBands]));
2437
+ }
2438
+ } while (++c < C);
2439
+
2440
+ if (silence)
2441
+ {
2442
+ for (i=0;i<C*nbEBands;i++)
2443
+ oldBandE[i] = -GCONST(28.f);
2444
+ }
2445
+
2446
+ #ifdef RESYNTH
2447
+ /* Re-synthesis of the coded audio if required */
2448
+ {
2449
+ celt_sig *out_mem[2];
2450
+
2451
+ if (anti_collapse_on)
2452
+ {
2453
+ anti_collapse(mode, X, collapse_masks, LM, C, N,
2454
+ start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
2455
+ }
2456
+
2457
+ c=0; do {
2458
+ OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2);
2459
+ } while (++c<CC);
2460
+
2461
+ c=0; do {
2462
+ out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
2463
+ } while (++c<CC);
2464
+
2465
+ celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2466
+ C, CC, isTransient, LM, st->upsample, silence, st->arch);
2467
+
2468
+ c=0; do {
2469
+ st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2470
+ st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2471
+ comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2472
+ st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2473
+ mode->window, overlap, st->arch);
2474
+ if (LM!=0)
2475
+ comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2476
+ st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2477
+ mode->window, overlap, st->arch);
2478
+ } while (++c<CC);
2479
+
2480
+ /* We reuse freq[] as scratch space for the de-emphasis */
2481
+ deemphasis(out_mem, (opus_res*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, 0);
2482
+ st->prefilter_period_old = st->prefilter_period;
2483
+ st->prefilter_gain_old = st->prefilter_gain;
2484
+ st->prefilter_tapset_old = st->prefilter_tapset;
2485
+ }
2486
+ #endif
2487
+
2488
+ st->prefilter_period = pitch_index;
2489
+ st->prefilter_gain = gain1;
2490
+ st->prefilter_tapset = prefilter_tapset;
2491
+ #ifdef RESYNTH
2492
+ if (LM!=0)
2493
+ {
2494
+ st->prefilter_period_old = st->prefilter_period;
2495
+ st->prefilter_gain_old = st->prefilter_gain;
2496
+ st->prefilter_tapset_old = st->prefilter_tapset;
2497
+ }
2498
+ #endif
2499
+
2500
+ if (CC==2&&C==1) {
2501
+ OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
2502
+ }
2503
+
2504
+ if (!isTransient)
2505
+ {
2506
+ OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2507
+ OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
2508
+ } else {
2509
+ for (i=0;i<CC*nbEBands;i++)
2510
+ oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
2511
+ }
2512
+ /* In case start or end were to change */
2513
+ c=0; do
2514
+ {
2515
+ for (i=0;i<start;i++)
2516
+ {
2517
+ oldBandE[c*nbEBands+i]=0;
2518
+ oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2519
+ }
2520
+ for (i=end;i<nbEBands;i++)
2521
+ {
2522
+ oldBandE[c*nbEBands+i]=0;
2523
+ oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2524
+ }
2525
+ } while (++c<CC);
2526
+
2527
+ if (isTransient || transient_got_disabled)
2528
+ st->consec_transient++;
2529
+ else
2530
+ st->consec_transient=0;
2531
+ st->rng = enc->rng;
2532
+
2533
+ /* If there's any room left (can only happen for very high rates),
2534
+ it's already filled with zeros */
2535
+ ec_enc_done(enc);
2536
+
2537
+ #ifdef CUSTOM_MODES
2538
+ if (st->signalling)
2539
+ nbCompressedBytes++;
2540
+ #endif
2541
+
2542
+ RESTORE_STACK;
2543
+ if (ec_get_error(enc))
2544
+ return OPUS_INTERNAL_ERROR;
2545
+ else
2546
+ return nbCompressedBytes;
2547
+ }
2548
+
2549
+
2550
+ #ifdef CUSTOM_MODES
2551
+
2552
+ #if defined(FIXED_POINT) && !defined(ENABLE_RES24)
2553
+ int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2554
+ {
2555
+ return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2556
+ }
2557
+ #else
2558
+ int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2559
+ {
2560
+ int j, ret, C, N;
2561
+ VARDECL(opus_res, in);
2562
+ ALLOC_STACK;
2563
+
2564
+ if (pcm==NULL)
2565
+ return OPUS_BAD_ARG;
2566
+
2567
+ C = st->channels;
2568
+ N = frame_size;
2569
+ ALLOC(in, C*N, opus_res);
2570
+
2571
+ for (j=0;j<C*N;j++)
2572
+ in[j] = INT16TORES(pcm[j]);
2573
+
2574
+ ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2575
+ #ifdef RESYNTH
2576
+ for (j=0;j<C*N;j++)
2577
+ ((opus_int16*)pcm)[j]=RES2INT16(in[j]);
2578
+ #endif
2579
+ RESTORE_STACK;
2580
+ return ret;
2581
+ }
2582
+ #endif
2583
+
2584
+
2585
+ #if defined(FIXED_POINT) && defined(ENABLE_RES24)
2586
+ int opus_custom_encode24(CELTEncoder * OPUS_RESTRICT st, const opus_int32 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2587
+ {
2588
+ return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2589
+ }
2590
+ #else
2591
+ int opus_custom_encode24(CELTEncoder * OPUS_RESTRICT st, const opus_int32 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2592
+ {
2593
+ int j, ret, C, N;
2594
+ VARDECL(opus_res, in);
2595
+ ALLOC_STACK;
2596
+
2597
+ if (pcm==NULL)
2598
+ return OPUS_BAD_ARG;
2599
+
2600
+ C = st->channels;
2601
+ N = frame_size;
2602
+ ALLOC(in, C*N, opus_res);
2603
+
2604
+ for (j=0;j<C*N;j++)
2605
+ in[j] = INT24TORES(pcm[j]);
2606
+
2607
+ ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2608
+ #ifdef RESYNTH
2609
+ for (j=0;j<C*N;j++)
2610
+ ((opus_int32*)pcm)[j]=RES2INT24(in[j]);
2611
+ #endif
2612
+ RESTORE_STACK;
2613
+ return ret;
2614
+ }
2615
+ #endif
2616
+
2617
+
2618
+ #ifndef DISABLE_FLOAT_API
2619
+
2620
+ # if !defined(FIXED_POINT)
2621
+ int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2622
+ {
2623
+ return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2624
+ }
2625
+ # else
2626
+ int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2627
+ {
2628
+ int j, ret, C, N;
2629
+ VARDECL(opus_res, in);
2630
+ ALLOC_STACK;
2631
+
2632
+ if (pcm==NULL)
2633
+ return OPUS_BAD_ARG;
2634
+
2635
+ C = st->channels;
2636
+ N = frame_size;
2637
+ ALLOC(in, C*N, opus_res);
2638
+
2639
+ for (j=0;j<C*N;j++)
2640
+ in[j] = FLOAT2RES(pcm[j]);
2641
+
2642
+ ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2643
+ #ifdef RESYNTH
2644
+ for (j=0;j<C*N;j++)
2645
+ ((float*)pcm)[j]=RES2FLOAT(in[j]);
2646
+ #endif
2647
+ RESTORE_STACK;
2648
+ return ret;
2649
+ }
2650
+ # endif
2651
+
2652
+ #endif
2653
+
2654
+ #endif /* CUSTOM_MODES */
2655
+
2656
+ int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2657
+ {
2658
+ va_list ap;
2659
+
2660
+ va_start(ap, request);
2661
+ switch (request)
2662
+ {
2663
+ case OPUS_SET_COMPLEXITY_REQUEST:
2664
+ {
2665
+ int value = va_arg(ap, opus_int32);
2666
+ if (value<0 || value>10)
2667
+ goto bad_arg;
2668
+ st->complexity = value;
2669
+ }
2670
+ break;
2671
+ case CELT_SET_START_BAND_REQUEST:
2672
+ {
2673
+ opus_int32 value = va_arg(ap, opus_int32);
2674
+ if (value<0 || value>=st->mode->nbEBands)
2675
+ goto bad_arg;
2676
+ st->start = value;
2677
+ }
2678
+ break;
2679
+ case CELT_SET_END_BAND_REQUEST:
2680
+ {
2681
+ opus_int32 value = va_arg(ap, opus_int32);
2682
+ if (value<1 || value>st->mode->nbEBands)
2683
+ goto bad_arg;
2684
+ st->end = value;
2685
+ }
2686
+ break;
2687
+ case CELT_SET_PREDICTION_REQUEST:
2688
+ {
2689
+ int value = va_arg(ap, opus_int32);
2690
+ if (value<0 || value>2)
2691
+ goto bad_arg;
2692
+ st->disable_pf = value<=1;
2693
+ st->force_intra = value==0;
2694
+ }
2695
+ break;
2696
+ case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2697
+ {
2698
+ int value = va_arg(ap, opus_int32);
2699
+ if (value<0 || value>100)
2700
+ goto bad_arg;
2701
+ st->loss_rate = value;
2702
+ }
2703
+ break;
2704
+ case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2705
+ {
2706
+ opus_int32 value = va_arg(ap, opus_int32);
2707
+ st->constrained_vbr = value;
2708
+ }
2709
+ break;
2710
+ case OPUS_SET_VBR_REQUEST:
2711
+ {
2712
+ opus_int32 value = va_arg(ap, opus_int32);
2713
+ st->vbr = value;
2714
+ }
2715
+ break;
2716
+ case OPUS_SET_BITRATE_REQUEST:
2717
+ {
2718
+ opus_int32 value = va_arg(ap, opus_int32);
2719
+ if (value<=500 && value!=OPUS_BITRATE_MAX)
2720
+ goto bad_arg;
2721
+ value = IMIN(value, 260000*st->channels);
2722
+ st->bitrate = value;
2723
+ }
2724
+ break;
2725
+ case CELT_SET_CHANNELS_REQUEST:
2726
+ {
2727
+ opus_int32 value = va_arg(ap, opus_int32);
2728
+ if (value<1 || value>2)
2729
+ goto bad_arg;
2730
+ st->stream_channels = value;
2731
+ }
2732
+ break;
2733
+ case OPUS_SET_LSB_DEPTH_REQUEST:
2734
+ {
2735
+ opus_int32 value = va_arg(ap, opus_int32);
2736
+ if (value<8 || value>24)
2737
+ goto bad_arg;
2738
+ st->lsb_depth=value;
2739
+ }
2740
+ break;
2741
+ case OPUS_GET_LSB_DEPTH_REQUEST:
2742
+ {
2743
+ opus_int32 *value = va_arg(ap, opus_int32*);
2744
+ *value=st->lsb_depth;
2745
+ }
2746
+ break;
2747
+ case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2748
+ {
2749
+ opus_int32 value = va_arg(ap, opus_int32);
2750
+ if(value<0 || value>1)
2751
+ {
2752
+ goto bad_arg;
2753
+ }
2754
+ st->disable_inv = value;
2755
+ }
2756
+ break;
2757
+ case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
2758
+ {
2759
+ opus_int32 *value = va_arg(ap, opus_int32*);
2760
+ if (!value)
2761
+ {
2762
+ goto bad_arg;
2763
+ }
2764
+ *value = st->disable_inv;
2765
+ }
2766
+ break;
2767
+ case OPUS_RESET_STATE:
2768
+ {
2769
+ int i;
2770
+ celt_glog *oldBandE, *oldLogE, *oldLogE2;
2771
+ oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+COMBFILTER_MAXPERIOD));
2772
+ oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2773
+ oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2774
+ OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2775
+ opus_custom_encoder_get_size(st->mode, st->channels)-
2776
+ ((char*)&st->ENCODER_RESET_START - (char*)st));
2777
+ for (i=0;i<st->channels*st->mode->nbEBands;i++)
2778
+ oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
2779
+ st->vbr_offset = 0;
2780
+ st->delayedIntra = 1;
2781
+ st->spread_decision = SPREAD_NORMAL;
2782
+ st->tonal_average = 256;
2783
+ st->hf_average = 0;
2784
+ st->tapset_decision = 0;
2785
+ }
2786
+ break;
2787
+ #ifdef CUSTOM_MODES
2788
+ case CELT_SET_INPUT_CLIPPING_REQUEST:
2789
+ {
2790
+ opus_int32 value = va_arg(ap, opus_int32);
2791
+ st->clip = value;
2792
+ }
2793
+ break;
2794
+ #endif
2795
+ case CELT_SET_SIGNALLING_REQUEST:
2796
+ {
2797
+ opus_int32 value = va_arg(ap, opus_int32);
2798
+ st->signalling = value;
2799
+ }
2800
+ break;
2801
+ case CELT_SET_ANALYSIS_REQUEST:
2802
+ {
2803
+ AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2804
+ if (info)
2805
+ OPUS_COPY(&st->analysis, info, 1);
2806
+ }
2807
+ break;
2808
+ case CELT_SET_SILK_INFO_REQUEST:
2809
+ {
2810
+ SILKInfo *info = va_arg(ap, SILKInfo *);
2811
+ if (info)
2812
+ OPUS_COPY(&st->silk_info, info, 1);
2813
+ }
2814
+ break;
2815
+ case CELT_GET_MODE_REQUEST:
2816
+ {
2817
+ const CELTMode ** value = va_arg(ap, const CELTMode**);
2818
+ if (value==0)
2819
+ goto bad_arg;
2820
+ *value=st->mode;
2821
+ }
2822
+ break;
2823
+ case OPUS_GET_FINAL_RANGE_REQUEST:
2824
+ {
2825
+ opus_uint32 * value = va_arg(ap, opus_uint32 *);
2826
+ if (value==0)
2827
+ goto bad_arg;
2828
+ *value=st->rng;
2829
+ }
2830
+ break;
2831
+ case OPUS_SET_LFE_REQUEST:
2832
+ {
2833
+ opus_int32 value = va_arg(ap, opus_int32);
2834
+ st->lfe = value;
2835
+ }
2836
+ break;
2837
+ case OPUS_SET_ENERGY_MASK_REQUEST:
2838
+ {
2839
+ celt_glog *value = va_arg(ap, celt_glog*);
2840
+ st->energy_mask = value;
2841
+ }
2842
+ break;
2843
+ default:
2844
+ goto bad_request;
2845
+ }
2846
+ va_end(ap);
2847
+ return OPUS_OK;
2848
+ bad_arg:
2849
+ va_end(ap);
2850
+ return OPUS_BAD_ARG;
2851
+ bad_request:
2852
+ va_end(ap);
2853
+ return OPUS_UNIMPLEMENTED;
2854
+ }