@scrypted/libav 1.0.166 → 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,3180 @@
1
+ /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2
+ Written by Jean-Marc Valin and Koen Vos */
3
+ /*
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ #ifdef HAVE_CONFIG_H
29
+ #include "config.h"
30
+ #endif
31
+
32
+ #include <stdarg.h>
33
+ #include "celt.h"
34
+ #include "entenc.h"
35
+ #include "modes.h"
36
+ #include "API.h"
37
+ #include "stack_alloc.h"
38
+ #include "float_cast.h"
39
+ #include "opus.h"
40
+ #include "arch.h"
41
+ #include "pitch.h"
42
+ #include "opus_private.h"
43
+ #include "os_support.h"
44
+ #include "cpu_support.h"
45
+ #include "analysis.h"
46
+ #include "mathops.h"
47
+ #include "tuning_parameters.h"
48
+
49
+ #ifdef ENABLE_DRED
50
+ #include "dred_coding.h"
51
+ #endif
52
+
53
+ #ifdef FIXED_POINT
54
+ #include "fixed/structs_FIX.h"
55
+ #else
56
+ #include "float/structs_FLP.h"
57
+ #endif
58
+ #ifdef ENABLE_OSCE_TRAINING_DATA
59
+ #include <stdio.h>
60
+ #endif
61
+
62
+ #define MAX_ENCODER_BUFFER 480
63
+
64
+ #ifndef DISABLE_FLOAT_API
65
+ #define PSEUDO_SNR_THRESHOLD 316.23f /* 10^(25/10) */
66
+ #endif
67
+
68
+ typedef struct {
69
+ opus_val32 XX, XY, YY;
70
+ opus_val16 smoothed_width;
71
+ opus_val16 max_follower;
72
+ } StereoWidthState;
73
+
74
+ struct OpusEncoder {
75
+ int celt_enc_offset;
76
+ int silk_enc_offset;
77
+ silk_EncControlStruct silk_mode;
78
+ #ifdef ENABLE_DRED
79
+ DREDEnc dred_encoder;
80
+ #endif
81
+ int application;
82
+ int channels;
83
+ int delay_compensation;
84
+ int force_channels;
85
+ int signal_type;
86
+ int user_bandwidth;
87
+ int max_bandwidth;
88
+ int user_forced_mode;
89
+ int voice_ratio;
90
+ opus_int32 Fs;
91
+ int use_vbr;
92
+ int vbr_constraint;
93
+ int variable_duration;
94
+ opus_int32 bitrate_bps;
95
+ opus_int32 user_bitrate_bps;
96
+ int lsb_depth;
97
+ int encoder_buffer;
98
+ int lfe;
99
+ int arch;
100
+ int use_dtx; /* general DTX for both SILK and CELT */
101
+ int fec_config;
102
+ #ifndef DISABLE_FLOAT_API
103
+ TonalityAnalysisState analysis;
104
+ #endif
105
+
106
+ #define OPUS_ENCODER_RESET_START stream_channels
107
+ int stream_channels;
108
+ opus_int16 hybrid_stereo_width_Q14;
109
+ opus_int32 variable_HP_smth2_Q15;
110
+ opus_val16 prev_HB_gain;
111
+ opus_val32 hp_mem[4];
112
+ int mode;
113
+ int prev_mode;
114
+ int prev_channels;
115
+ int prev_framesize;
116
+ int bandwidth;
117
+ /* Bandwidth determined automatically from the rate (before any other adjustment) */
118
+ int auto_bandwidth;
119
+ int silk_bw_switch;
120
+ /* Sampling rate (at the API level) */
121
+ int first;
122
+ celt_glog * energy_masking;
123
+ StereoWidthState width_mem;
124
+ opus_res delay_buffer[MAX_ENCODER_BUFFER*2];
125
+ #ifndef DISABLE_FLOAT_API
126
+ int detected_bandwidth;
127
+ int nb_no_activity_ms_Q1;
128
+ opus_val32 peak_signal_energy;
129
+ #endif
130
+ #ifdef ENABLE_DRED
131
+ int dred_duration;
132
+ int dred_q0;
133
+ int dred_dQ;
134
+ int dred_qmax;
135
+ int dred_target_chunks;
136
+ unsigned char activity_mem[DRED_MAX_FRAMES*4]; /* 2.5ms resolution*/
137
+ #endif
138
+ int nonfinal_frame; /* current frame is not the final in a packet */
139
+ opus_uint32 rangeFinal;
140
+ };
141
+
142
+ /* Transition tables for the voice and music. First column is the
143
+ middle (memoriless) threshold. The second column is the hysteresis
144
+ (difference with the middle) */
145
+ static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
146
+ 9000, 700, /* NB<->MB */
147
+ 9000, 700, /* MB<->WB */
148
+ 13500, 1000, /* WB<->SWB */
149
+ 14000, 2000, /* SWB<->FB */
150
+ };
151
+ static const opus_int32 mono_music_bandwidth_thresholds[8] = {
152
+ 9000, 700, /* NB<->MB */
153
+ 9000, 700, /* MB<->WB */
154
+ 11000, 1000, /* WB<->SWB */
155
+ 12000, 2000, /* SWB<->FB */
156
+ };
157
+ static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
158
+ 9000, 700, /* NB<->MB */
159
+ 9000, 700, /* MB<->WB */
160
+ 13500, 1000, /* WB<->SWB */
161
+ 14000, 2000, /* SWB<->FB */
162
+ };
163
+ static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
164
+ 9000, 700, /* NB<->MB */
165
+ 9000, 700, /* MB<->WB */
166
+ 11000, 1000, /* WB<->SWB */
167
+ 12000, 2000, /* SWB<->FB */
168
+ };
169
+ /* Threshold bit-rates for switching between mono and stereo */
170
+ static const opus_int32 stereo_voice_threshold = 19000;
171
+ static const opus_int32 stereo_music_threshold = 17000;
172
+
173
+ /* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
174
+ static const opus_int32 mode_thresholds[2][2] = {
175
+ /* voice */ /* music */
176
+ { 64000, 10000}, /* mono */
177
+ { 44000, 10000}, /* stereo */
178
+ };
179
+
180
+ static const opus_int32 fec_thresholds[] = {
181
+ 12000, 1000, /* NB */
182
+ 14000, 1000, /* MB */
183
+ 16000, 1000, /* WB */
184
+ 20000, 1000, /* SWB */
185
+ 22000, 1000, /* FB */
186
+ };
187
+
188
+ int opus_encoder_get_size(int channels)
189
+ {
190
+ int silkEncSizeBytes, celtEncSizeBytes;
191
+ int ret;
192
+ if (channels<1 || channels > 2)
193
+ return 0;
194
+ ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
195
+ if (ret)
196
+ return 0;
197
+ silkEncSizeBytes = align(silkEncSizeBytes);
198
+ celtEncSizeBytes = celt_encoder_get_size(channels);
199
+ return align(sizeof(OpusEncoder))+silkEncSizeBytes+celtEncSizeBytes;
200
+ }
201
+
202
+ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
203
+ {
204
+ void *silk_enc;
205
+ CELTEncoder *celt_enc;
206
+ int err;
207
+ int ret, silkEncSizeBytes;
208
+
209
+ if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
210
+ (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
211
+ && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
212
+ return OPUS_BAD_ARG;
213
+
214
+ OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
215
+ /* Create SILK encoder */
216
+ ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
217
+ if (ret)
218
+ return OPUS_BAD_ARG;
219
+ silkEncSizeBytes = align(silkEncSizeBytes);
220
+ st->silk_enc_offset = align(sizeof(OpusEncoder));
221
+ st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
222
+ silk_enc = (char*)st+st->silk_enc_offset;
223
+ celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
224
+
225
+ st->stream_channels = st->channels = channels;
226
+
227
+ st->Fs = Fs;
228
+
229
+ st->arch = opus_select_arch();
230
+
231
+ ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode );
232
+ if(ret)return OPUS_INTERNAL_ERROR;
233
+
234
+ /* default SILK parameters */
235
+ st->silk_mode.nChannelsAPI = channels;
236
+ st->silk_mode.nChannelsInternal = channels;
237
+ st->silk_mode.API_sampleRate = st->Fs;
238
+ st->silk_mode.maxInternalSampleRate = 16000;
239
+ st->silk_mode.minInternalSampleRate = 8000;
240
+ st->silk_mode.desiredInternalSampleRate = 16000;
241
+ st->silk_mode.payloadSize_ms = 20;
242
+ st->silk_mode.bitRate = 25000;
243
+ st->silk_mode.packetLossPercentage = 0;
244
+ st->silk_mode.complexity = 9;
245
+ st->silk_mode.useInBandFEC = 0;
246
+ st->silk_mode.useDRED = 0;
247
+ st->silk_mode.useDTX = 0;
248
+ st->silk_mode.useCBR = 0;
249
+ st->silk_mode.reducedDependency = 0;
250
+
251
+ /* Create CELT encoder */
252
+ /* Initialize CELT encoder */
253
+ err = celt_encoder_init(celt_enc, Fs, channels, st->arch);
254
+ if(err!=OPUS_OK)return OPUS_INTERNAL_ERROR;
255
+
256
+ celt_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0));
257
+ celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity));
258
+
259
+ #ifdef ENABLE_DRED
260
+ /* Initialize DRED Encoder */
261
+ dred_encoder_init( &st->dred_encoder, Fs, channels );
262
+ #endif
263
+
264
+ st->use_vbr = 1;
265
+ /* Makes constrained VBR the default (safer for real-time use) */
266
+ st->vbr_constraint = 1;
267
+ st->user_bitrate_bps = OPUS_AUTO;
268
+ st->bitrate_bps = 3000+Fs*channels;
269
+ st->application = application;
270
+ st->signal_type = OPUS_AUTO;
271
+ st->user_bandwidth = OPUS_AUTO;
272
+ st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
273
+ st->force_channels = OPUS_AUTO;
274
+ st->user_forced_mode = OPUS_AUTO;
275
+ st->voice_ratio = -1;
276
+ st->encoder_buffer = st->Fs/100;
277
+ st->lsb_depth = 24;
278
+ st->variable_duration = OPUS_FRAMESIZE_ARG;
279
+
280
+ /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
281
+ + 1.5 ms for SILK resamplers and stereo prediction) */
282
+ st->delay_compensation = st->Fs/250;
283
+
284
+ st->hybrid_stereo_width_Q14 = 1 << 14;
285
+ st->prev_HB_gain = Q15ONE;
286
+ st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
287
+ st->first = 1;
288
+ st->mode = MODE_HYBRID;
289
+ st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
290
+
291
+ #ifndef DISABLE_FLOAT_API
292
+ tonality_analysis_init(&st->analysis, st->Fs);
293
+ st->analysis.application = st->application;
294
+ #endif
295
+
296
+ return OPUS_OK;
297
+ }
298
+
299
+ static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
300
+ {
301
+ int period;
302
+ unsigned char toc;
303
+ period = 0;
304
+ while (framerate < 400)
305
+ {
306
+ framerate <<= 1;
307
+ period++;
308
+ }
309
+ if (mode == MODE_SILK_ONLY)
310
+ {
311
+ toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND)<<5;
312
+ toc |= (period-2)<<3;
313
+ } else if (mode == MODE_CELT_ONLY)
314
+ {
315
+ int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND;
316
+ if (tmp < 0)
317
+ tmp = 0;
318
+ toc = 0x80;
319
+ toc |= tmp << 5;
320
+ toc |= period<<3;
321
+ } else /* Hybrid */
322
+ {
323
+ toc = 0x60;
324
+ toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4;
325
+ toc |= (period-2)<<3;
326
+ }
327
+ toc |= (channels==2)<<2;
328
+ return toc;
329
+ }
330
+
331
+ #ifdef FIXED_POINT
332
+ /* Second order ARMA filter, alternative implementation */
333
+ void silk_biquad_res(
334
+ const opus_res *in, /* I input signal */
335
+ const opus_int32 *B_Q28, /* I MA coefficients [3] */
336
+ const opus_int32 *A_Q28, /* I AR coefficients [2] */
337
+ opus_int32 *S, /* I/O State vector [2] */
338
+ opus_res *out, /* O output signal */
339
+ const opus_int32 len, /* I signal length (must be even) */
340
+ int stride
341
+ )
342
+ {
343
+ /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
344
+ opus_int k;
345
+ opus_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14;
346
+
347
+ /* Negate A_Q28 values and split in two parts */
348
+ A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF; /* lower part */
349
+ A0_U_Q28 = silk_RSHIFT( -A_Q28[ 0 ], 14 ); /* upper part */
350
+ A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF; /* lower part */
351
+ A1_U_Q28 = silk_RSHIFT( -A_Q28[ 1 ], 14 ); /* upper part */
352
+
353
+ for( k = 0; k < len; k++ ) {
354
+ /* S[ 0 ], S[ 1 ]: Q12 */
355
+ inval = RES2INT16(in[ k*stride ]);
356
+ out32_Q14 = silk_LSHIFT( silk_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 );
357
+
358
+ S[ 0 ] = S[1] + silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A0_L_Q28 ), 14 );
359
+ S[ 0 ] = silk_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 );
360
+ S[ 0 ] = silk_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval);
361
+
362
+ S[ 1 ] = silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A1_L_Q28 ), 14 );
363
+ S[ 1 ] = silk_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 );
364
+ S[ 1 ] = silk_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval );
365
+
366
+ /* Scale back to Q0 and saturate */
367
+ out[ k*stride ] = INT16TORES( silk_SAT16( silk_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) ) );
368
+ }
369
+ }
370
+ #else
371
+ static void silk_biquad_res(
372
+ const opus_res *in, /* I: Input signal */
373
+ const opus_int32 *B_Q28, /* I: MA coefficients [3] */
374
+ const opus_int32 *A_Q28, /* I: AR coefficients [2] */
375
+ opus_val32 *S, /* I/O: State vector [2] */
376
+ opus_res *out, /* O: Output signal */
377
+ const opus_int32 len, /* I: Signal length (must be even) */
378
+ int stride
379
+ )
380
+ {
381
+ /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
382
+ opus_int k;
383
+ opus_val32 vout;
384
+ opus_val32 inval;
385
+ opus_val32 A[2], B[3];
386
+
387
+ A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
388
+ A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
389
+ B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
390
+ B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
391
+ B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
392
+
393
+ /* Negate A_Q28 values and split in two parts */
394
+
395
+ for( k = 0; k < len; k++ ) {
396
+ /* S[ 0 ], S[ 1 ]: Q12 */
397
+ inval = in[ k*stride ];
398
+ vout = S[ 0 ] + B[0]*inval;
399
+
400
+ S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
401
+
402
+ S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL;
403
+
404
+ /* Scale back to Q0 and saturate */
405
+ out[ k*stride ] = vout;
406
+ }
407
+ }
408
+ #endif
409
+
410
+ static void hp_cutoff(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch)
411
+ {
412
+ opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
413
+ opus_int32 Fc_Q19, r_Q28, r_Q22;
414
+ (void)arch;
415
+
416
+ silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) );
417
+ Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 );
418
+ silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 );
419
+
420
+ r_Q28 = SILK_FIX_CONST( 1.0, 28 ) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 );
421
+
422
+ /* b = r * [ 1; -2; 1 ]; */
423
+ /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
424
+ B_Q28[ 0 ] = r_Q28;
425
+ B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 );
426
+ B_Q28[ 2 ] = r_Q28;
427
+
428
+ /* -r * ( 2 - Fc * Fc ); */
429
+ r_Q22 = silk_RSHIFT( r_Q28, 6 );
430
+ A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) );
431
+ A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 );
432
+
433
+ #if defined(FIXED_POINT) && !defined(ENABLE_RES24)
434
+ if( channels == 1 ) {
435
+ silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len );
436
+ } else {
437
+ silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch );
438
+ }
439
+ #else
440
+ silk_biquad_res( in, B_Q28, A_Q28, hp_mem, out, len, channels );
441
+ if( channels == 2 ) {
442
+ silk_biquad_res( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
443
+ }
444
+ #endif
445
+ }
446
+
447
+ #ifdef FIXED_POINT
448
+ static void dc_reject(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
449
+ {
450
+ int c, i;
451
+ int shift;
452
+
453
+ /* Approximates -round(log2(6.3*cutoff_Hz/Fs)) */
454
+ shift=celt_ilog2(Fs/(cutoff_Hz*4));
455
+ for (c=0;c<channels;c++)
456
+ {
457
+ for (i=0;i<len;i++)
458
+ {
459
+ opus_val32 x, y;
460
+ x = SHL32((opus_val32)in[channels*i+c], 14-RES_SHIFT);
461
+ y = x-hp_mem[2*c];
462
+ hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift);
463
+ #ifdef ENABLE_RES24
464
+ /* Don't saturate if we have the headroom to avoid it. */
465
+ out[channels*i+c] = PSHR32(y, 14-RES_SHIFT);
466
+ #else
467
+ out[channels*i+c] = SATURATE(PSHR32(y, 14-RES_SHIFT), 32767);
468
+ #endif
469
+ }
470
+ }
471
+ }
472
+
473
+ #else
474
+ static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
475
+ {
476
+ int i;
477
+ float coef, coef2;
478
+ coef = 6.3f*cutoff_Hz/Fs;
479
+ coef2 = 1-coef;
480
+ if (channels==2)
481
+ {
482
+ float m0, m2;
483
+ m0 = hp_mem[0];
484
+ m2 = hp_mem[2];
485
+ for (i=0;i<len;i++)
486
+ {
487
+ opus_val32 x0, x1, out0, out1;
488
+ x0 = in[2*i+0];
489
+ x1 = in[2*i+1];
490
+ out0 = x0-m0;
491
+ out1 = x1-m2;
492
+ m0 = coef*x0 + VERY_SMALL + coef2*m0;
493
+ m2 = coef*x1 + VERY_SMALL + coef2*m2;
494
+ out[2*i+0] = out0;
495
+ out[2*i+1] = out1;
496
+ }
497
+ hp_mem[0] = m0;
498
+ hp_mem[2] = m2;
499
+ } else {
500
+ float m0;
501
+ m0 = hp_mem[0];
502
+ for (i=0;i<len;i++)
503
+ {
504
+ opus_val32 x, y;
505
+ x = in[i];
506
+ y = x-m0;
507
+ m0 = coef*x + VERY_SMALL + coef2*m0;
508
+ out[i] = y;
509
+ }
510
+ hp_mem[0] = m0;
511
+ }
512
+ }
513
+ #endif
514
+
515
+ static void stereo_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2,
516
+ int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs)
517
+ {
518
+ int i;
519
+ int overlap;
520
+ int inc;
521
+ inc = 48000/Fs;
522
+ overlap=overlap48/inc;
523
+ g1 = Q15ONE-g1;
524
+ g2 = Q15ONE-g2;
525
+ for (i=0;i<overlap;i++)
526
+ {
527
+ opus_val32 diff;
528
+ opus_val16 g, w;
529
+ w = COEF2VAL16(window[i*inc]);
530
+ w = MULT16_16_Q15(w, w);
531
+ g = SHR32(MAC16_16(MULT16_16(w,g2),
532
+ Q15ONE-w, g1), 15);
533
+ diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
534
+ diff = MULT16_RES_Q15(g, diff);
535
+ out[i*channels] = out[i*channels] - diff;
536
+ out[i*channels+1] = out[i*channels+1] + diff;
537
+ }
538
+ for (;i<frame_size;i++)
539
+ {
540
+ opus_val32 diff;
541
+ diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1]);
542
+ diff = MULT16_RES_Q15(g2, diff);
543
+ out[i*channels] = out[i*channels] - diff;
544
+ out[i*channels+1] = out[i*channels+1] + diff;
545
+ }
546
+ }
547
+
548
+ static void gain_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2,
549
+ int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs)
550
+ {
551
+ int i;
552
+ int inc;
553
+ int overlap;
554
+ int c;
555
+ inc = 48000/Fs;
556
+ overlap=overlap48/inc;
557
+ if (channels==1)
558
+ {
559
+ for (i=0;i<overlap;i++)
560
+ {
561
+ opus_val16 g, w;
562
+ w = COEF2VAL16(window[i*inc]);
563
+ w = MULT16_16_Q15(w, w);
564
+ g = SHR32(MAC16_16(MULT16_16(w,g2),
565
+ Q15ONE-w, g1), 15);
566
+ out[i] = MULT16_RES_Q15(g, in[i]);
567
+ }
568
+ } else {
569
+ for (i=0;i<overlap;i++)
570
+ {
571
+ opus_val16 g, w;
572
+ w = COEF2VAL16(window[i*inc]);
573
+ w = MULT16_16_Q15(w, w);
574
+ g = SHR32(MAC16_16(MULT16_16(w,g2),
575
+ Q15ONE-w, g1), 15);
576
+ out[i*2] = MULT16_RES_Q15(g, in[i*2]);
577
+ out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1]);
578
+ }
579
+ }
580
+ c=0;do {
581
+ for (i=overlap;i<frame_size;i++)
582
+ {
583
+ out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c]);
584
+ }
585
+ }
586
+ while (++c<channels);
587
+ }
588
+
589
+ OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
590
+ {
591
+ int ret;
592
+ OpusEncoder *st;
593
+ if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000)||(channels!=1&&channels!=2)||
594
+ (application != OPUS_APPLICATION_VOIP && application != OPUS_APPLICATION_AUDIO
595
+ && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY))
596
+ {
597
+ if (error)
598
+ *error = OPUS_BAD_ARG;
599
+ return NULL;
600
+ }
601
+ st = (OpusEncoder *)opus_alloc(opus_encoder_get_size(channels));
602
+ if (st == NULL)
603
+ {
604
+ if (error)
605
+ *error = OPUS_ALLOC_FAIL;
606
+ return NULL;
607
+ }
608
+ ret = opus_encoder_init(st, Fs, channels, application);
609
+ if (error)
610
+ *error = ret;
611
+ if (ret != OPUS_OK)
612
+ {
613
+ opus_free(st);
614
+ st = NULL;
615
+ }
616
+ return st;
617
+ }
618
+
619
+ #ifdef ENABLE_DRED
620
+
621
+ static const float dred_bits_table[16] = {73.2f, 68.1f, 62.5f, 57.0f, 51.5f, 45.7f, 39.9f, 32.4f, 26.4f, 20.4f, 16.3f, 13.f, 9.3f, 8.2f, 7.2f, 6.4f};
622
+ static int estimate_dred_bitrate(int q0, int dQ, int qmax, int duration, opus_int32 target_bits, int *target_chunks) {
623
+ int dred_chunks;
624
+ int i;
625
+ float bits;
626
+ /* Signaling DRED costs 3 bytes. */
627
+ bits = 8*(3+DRED_EXPERIMENTAL_BYTES);
628
+ /* Approximation for the size of the IS. */
629
+ bits += 50.f+dred_bits_table[q0];
630
+ dred_chunks = IMIN((duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
631
+ if (target_chunks != NULL) *target_chunks = 0;
632
+ for (i=0;i<dred_chunks;i++) {
633
+ int q = compute_quantizer(q0, dQ, qmax, i);
634
+ bits += dred_bits_table[q];
635
+ if (target_chunks != NULL && bits < target_bits) *target_chunks = i+1;
636
+ }
637
+ return (int)floor(.5f+bits);
638
+ }
639
+
640
+ static opus_int32 compute_dred_bitrate(OpusEncoder *st, opus_int32 bitrate_bps, int frame_size)
641
+ {
642
+ float dred_frac;
643
+ int bitrate_offset;
644
+ opus_int32 dred_bitrate;
645
+ opus_int32 target_dred_bitrate;
646
+ int target_chunks;
647
+ opus_int32 max_dred_bits;
648
+ int q0, dQ, qmax;
649
+ if (st->silk_mode.useInBandFEC) {
650
+ dred_frac = MIN16(.7f, 3.f*st->silk_mode.packetLossPercentage/100.f);
651
+ bitrate_offset = 20000;
652
+ } else {
653
+ if (st->silk_mode.packetLossPercentage > 5) {
654
+ dred_frac = MIN16(.8f, .55f + st->silk_mode.packetLossPercentage/100.f);
655
+ } else {
656
+ dred_frac = 12*st->silk_mode.packetLossPercentage/100.f;
657
+ }
658
+ bitrate_offset = 12000;
659
+ }
660
+ /* Account for the fact that longer packets require less redundancy. */
661
+ dred_frac = dred_frac/(dred_frac + (1-dred_frac)*(frame_size*50.f)/st->Fs);
662
+ /* Approximate fit based on a few experiments. Could probably be improved. */
663
+ q0 = IMIN(15, IMAX(4, 51 - 3*EC_ILOG(IMAX(1, bitrate_bps-bitrate_offset))));
664
+ dQ = bitrate_bps-bitrate_offset > 36000 ? 3 : 5;
665
+ qmax = 15;
666
+ target_dred_bitrate = IMAX(0, (int)(dred_frac*(bitrate_bps-bitrate_offset)));
667
+ if (st->dred_duration > 0) {
668
+ opus_int32 target_bits = target_dred_bitrate*frame_size/st->Fs;
669
+ max_dred_bits = estimate_dred_bitrate(q0, dQ, qmax, st->dred_duration, target_bits, &target_chunks);
670
+ } else {
671
+ max_dred_bits = 0;
672
+ target_chunks=0;
673
+ }
674
+ dred_bitrate = IMIN(target_dred_bitrate, max_dred_bits*st->Fs/frame_size);
675
+ /* If we can't afford enough bits, don't bother with DRED at all. */
676
+ if (target_chunks < 2)
677
+ dred_bitrate = 0;
678
+ st->dred_q0 = q0;
679
+ st->dred_dQ = dQ;
680
+ st->dred_qmax = qmax;
681
+ st->dred_target_chunks = target_chunks;
682
+ return dred_bitrate;
683
+ }
684
+ #endif
685
+
686
+ static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
687
+ {
688
+ if(!frame_size)frame_size=st->Fs/400;
689
+ if (st->user_bitrate_bps==OPUS_AUTO)
690
+ return 60*st->Fs/frame_size + st->Fs*st->channels;
691
+ else if (st->user_bitrate_bps==OPUS_BITRATE_MAX)
692
+ return max_data_bytes*8*st->Fs/frame_size;
693
+ else
694
+ return st->user_bitrate_bps;
695
+ }
696
+
697
+ #ifndef DISABLE_FLOAT_API
698
+ void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
699
+ {
700
+ const float *x;
701
+ int j;
702
+
703
+ x = (const float *)_x;
704
+ for (j=0;j<subframe;j++)
705
+ y[j] = FLOAT2SIG(x[(j+offset)*C+c1]);
706
+ if (c2>-1)
707
+ {
708
+ for (j=0;j<subframe;j++)
709
+ y[j] += FLOAT2SIG(x[(j+offset)*C+c2]);
710
+ } else if (c2==-2)
711
+ {
712
+ int c;
713
+ for (c=1;c<C;c++)
714
+ {
715
+ for (j=0;j<subframe;j++)
716
+ y[j] += FLOAT2SIG(x[(j+offset)*C+c]);
717
+ }
718
+ }
719
+ }
720
+ #endif
721
+
722
+ void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
723
+ {
724
+ const opus_int16 *x;
725
+ int j;
726
+
727
+ x = (const opus_int16 *)_x;
728
+ for (j=0;j<subframe;j++)
729
+ y[j] = INT16TOSIG(x[(j+offset)*C+c1]);
730
+ if (c2>-1)
731
+ {
732
+ for (j=0;j<subframe;j++)
733
+ y[j] += INT16TOSIG(x[(j+offset)*C+c2]);
734
+ } else if (c2==-2)
735
+ {
736
+ int c;
737
+ for (c=1;c<C;c++)
738
+ {
739
+ for (j=0;j<subframe;j++)
740
+ y[j] += INT16TOSIG(x[(j+offset)*C+c]);
741
+ }
742
+ }
743
+ }
744
+
745
+ void downmix_int24(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
746
+ {
747
+ const opus_int32 *x;
748
+ int j;
749
+
750
+ x = (const opus_int32 *)_x;
751
+ for (j=0;j<subframe;j++)
752
+ y[j] = INT24TOSIG(x[(j+offset)*C+c1]);
753
+ if (c2>-1)
754
+ {
755
+ for (j=0;j<subframe;j++)
756
+ y[j] += INT24TOSIG(x[(j+offset)*C+c2]);
757
+ } else if (c2==-2)
758
+ {
759
+ int c;
760
+ for (c=1;c<C;c++)
761
+ {
762
+ for (j=0;j<subframe;j++)
763
+ y[j] += INT24TOSIG(x[(j+offset)*C+c]);
764
+ }
765
+ }
766
+ }
767
+
768
+ opus_int32 frame_size_select(opus_int32 frame_size, int variable_duration, opus_int32 Fs)
769
+ {
770
+ int new_size;
771
+ if (frame_size<Fs/400)
772
+ return -1;
773
+ if (variable_duration == OPUS_FRAMESIZE_ARG)
774
+ new_size = frame_size;
775
+ else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS && variable_duration <= OPUS_FRAMESIZE_120_MS)
776
+ {
777
+ if (variable_duration <= OPUS_FRAMESIZE_40_MS)
778
+ new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS);
779
+ else
780
+ new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS-2)*Fs/50;
781
+ }
782
+ else
783
+ return -1;
784
+ if (new_size>frame_size)
785
+ return -1;
786
+ if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
787
+ 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs &&
788
+ 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs)
789
+ return -1;
790
+ return new_size;
791
+ }
792
+
793
+ opus_val16 compute_stereo_width(const opus_res *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem)
794
+ {
795
+ opus_val32 xx, xy, yy;
796
+ opus_val16 sqrt_xx, sqrt_yy;
797
+ opus_val16 qrrt_xx, qrrt_yy;
798
+ int frame_rate;
799
+ int i;
800
+ opus_val16 short_alpha;
801
+
802
+ frame_rate = Fs/frame_size;
803
+ short_alpha = Q15ONE - MULT16_16(25, Q15ONE)/IMAX(50,frame_rate);
804
+ xx=xy=yy=0;
805
+ /* Unroll by 4. The frame size is always a multiple of 4 *except* for
806
+ 2.5 ms frames at 12 kHz. Since this setting is very rare (and very
807
+ stupid), we just discard the last two samples. */
808
+ for (i=0;i<frame_size-3;i+=4)
809
+ {
810
+ opus_val32 pxx=0;
811
+ opus_val32 pxy=0;
812
+ opus_val32 pyy=0;
813
+ opus_val16 x, y;
814
+ x = RES2VAL16(pcm[2*i]);
815
+ y = RES2VAL16(pcm[2*i+1]);
816
+ pxx = SHR32(MULT16_16(x,x),2);
817
+ pxy = SHR32(MULT16_16(x,y),2);
818
+ pyy = SHR32(MULT16_16(y,y),2);
819
+ x = RES2VAL16(pcm[2*i+2]);
820
+ y = RES2VAL16(pcm[2*i+3]);
821
+ pxx += SHR32(MULT16_16(x,x),2);
822
+ pxy += SHR32(MULT16_16(x,y),2);
823
+ pyy += SHR32(MULT16_16(y,y),2);
824
+ x = RES2VAL16(pcm[2*i+4]);
825
+ y = RES2VAL16(pcm[2*i+5]);
826
+ pxx += SHR32(MULT16_16(x,x),2);
827
+ pxy += SHR32(MULT16_16(x,y),2);
828
+ pyy += SHR32(MULT16_16(y,y),2);
829
+ x = RES2VAL16(pcm[2*i+6]);
830
+ y = RES2VAL16(pcm[2*i+7]);
831
+ pxx += SHR32(MULT16_16(x,x),2);
832
+ pxy += SHR32(MULT16_16(x,y),2);
833
+ pyy += SHR32(MULT16_16(y,y),2);
834
+
835
+ xx += SHR32(pxx, 10);
836
+ xy += SHR32(pxy, 10);
837
+ yy += SHR32(pyy, 10);
838
+ }
839
+ #ifndef FIXED_POINT
840
+ if (!(xx < 1e9f) || celt_isnan(xx) || !(yy < 1e9f) || celt_isnan(yy))
841
+ {
842
+ xy = xx = yy = 0;
843
+ }
844
+ #endif
845
+ mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX);
846
+ mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);
847
+ mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY);
848
+ mem->XX = MAX32(0, mem->XX);
849
+ mem->XY = MAX32(0, mem->XY);
850
+ mem->YY = MAX32(0, mem->YY);
851
+ if (MAX32(mem->XX, mem->YY)>QCONST16(8e-4f, 18))
852
+ {
853
+ opus_val16 corr;
854
+ opus_val16 ldiff;
855
+ opus_val16 width;
856
+ sqrt_xx = celt_sqrt(mem->XX);
857
+ sqrt_yy = celt_sqrt(mem->YY);
858
+ qrrt_xx = celt_sqrt(sqrt_xx);
859
+ qrrt_yy = celt_sqrt(sqrt_yy);
860
+ /* Inter-channel correlation */
861
+ mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy);
862
+ corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16);
863
+ /* Approximate loudness difference */
864
+ ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))/(EPSILON+qrrt_xx+qrrt_yy);
865
+ width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff);
866
+ /* Smoothing over one second */
867
+ mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
868
+ /* Peak follower */
869
+ mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width);
870
+ }
871
+ /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/
872
+ return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)));
873
+ }
874
+
875
+ static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate)
876
+ {
877
+ int orig_bandwidth;
878
+ if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY)
879
+ return 0;
880
+ orig_bandwidth = *bandwidth;
881
+ for (;;)
882
+ {
883
+ opus_int32 hysteresis;
884
+ opus_int32 LBRR_rate_thres_bps;
885
+ /* Compute threshold for using FEC at the current bandwidth setting */
886
+ LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND)];
887
+ hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1];
888
+ if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis;
889
+ if (last_fec == 0) LBRR_rate_thres_bps += hysteresis;
890
+ LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps,
891
+ 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) );
892
+ /* If loss <= 5%, we look at whether we have enough rate to enable FEC.
893
+ If loss > 5%, we decrease the bandwidth until we can enable FEC. */
894
+ if (rate > LBRR_rate_thres_bps)
895
+ return 1;
896
+ else if (PacketLoss_perc <= 5)
897
+ return 0;
898
+ else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND)
899
+ (*bandwidth)--;
900
+ else
901
+ break;
902
+ }
903
+ /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */
904
+ *bandwidth = orig_bandwidth;
905
+ return 0;
906
+ }
907
+
908
+ static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec, int channels) {
909
+ int entry;
910
+ int i;
911
+ int N;
912
+ int silk_rate;
913
+ static int rate_table[][5] = {
914
+ /* |total| |-------- SILK------------|
915
+ |-- No FEC -| |--- FEC ---|
916
+ 10ms 20ms 10ms 20ms */
917
+ { 0, 0, 0, 0, 0},
918
+ {12000, 10000, 10000, 11000, 11000},
919
+ {16000, 13500, 13500, 15000, 15000},
920
+ {20000, 16000, 16000, 18000, 18000},
921
+ {24000, 18000, 18000, 21000, 21000},
922
+ {32000, 22000, 22000, 28000, 28000},
923
+ {64000, 38000, 38000, 50000, 50000}
924
+ };
925
+ /* Do the allocation per-channel. */
926
+ rate /= channels;
927
+ entry = 1 + frame20ms + 2*fec;
928
+ N = sizeof(rate_table)/sizeof(rate_table[0]);
929
+ for (i=1;i<N;i++)
930
+ {
931
+ if (rate_table[i][0] > rate) break;
932
+ }
933
+ if (i == N)
934
+ {
935
+ silk_rate = rate_table[i-1][entry];
936
+ /* For now, just give 50% of the extra bits to SILK. */
937
+ silk_rate += (rate-rate_table[i-1][0])/2;
938
+ } else {
939
+ opus_int32 lo, hi, x0, x1;
940
+ lo = rate_table[i-1][entry];
941
+ hi = rate_table[i][entry];
942
+ x0 = rate_table[i-1][0];
943
+ x1 = rate_table[i][0];
944
+ silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0);
945
+ }
946
+ if (!vbr)
947
+ {
948
+ /* Tiny boost to SILK for CBR. We should probably tune this better. */
949
+ silk_rate += 100;
950
+ }
951
+ if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND)
952
+ silk_rate += 300;
953
+ silk_rate *= channels;
954
+ /* Small adjustment for stereo (calibrated for 32 kb/s, haven't tried other bitrates). */
955
+ if (channels == 2 && rate >= 12000)
956
+ silk_rate -= 1000;
957
+ return silk_rate;
958
+ }
959
+
960
+ /* Returns the equivalent bitrate corresponding to 20 ms frames,
961
+ complexity 10 VBR operation. */
962
+ static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels,
963
+ int frame_rate, int vbr, int mode, int complexity, int loss)
964
+ {
965
+ opus_int32 equiv;
966
+ equiv = bitrate;
967
+ /* Take into account overhead from smaller frames. */
968
+ if (frame_rate > 50)
969
+ equiv -= (40*channels+20)*(frame_rate - 50);
970
+ /* CBR is about a 8% penalty for both SILK and CELT. */
971
+ if (!vbr)
972
+ equiv -= equiv/12;
973
+ /* Complexity makes about 10% difference (from 0 to 10) in general. */
974
+ equiv = equiv * (90+complexity)/100;
975
+ if (mode == MODE_SILK_ONLY || mode == MODE_HYBRID)
976
+ {
977
+ /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which
978
+ costs about 20%. */
979
+ if (complexity<2)
980
+ equiv = equiv*4/5;
981
+ equiv -= equiv*loss/(6*loss + 10);
982
+ } else if (mode == MODE_CELT_ONLY) {
983
+ /* CELT complexity 0-4 doesn't have the pitch filter, which costs
984
+ about 10%. */
985
+ if (complexity<5)
986
+ equiv = equiv*9/10;
987
+ } else {
988
+ /* Mode not known yet */
989
+ /* Half the SILK loss*/
990
+ equiv -= equiv*loss/(12*loss + 20);
991
+ }
992
+ return equiv;
993
+ }
994
+
995
+ #ifndef DISABLE_FLOAT_API
996
+
997
+ int is_digital_silence(const opus_res* pcm, int frame_size, int channels, int lsb_depth)
998
+ {
999
+ int silence = 0;
1000
+ opus_val32 sample_max = 0;
1001
+ #ifdef MLP_TRAINING
1002
+ return 0;
1003
+ #endif
1004
+ sample_max = celt_maxabs_res(pcm, frame_size*channels);
1005
+
1006
+ #ifdef FIXED_POINT
1007
+ silence = (sample_max == 0);
1008
+ (void)lsb_depth;
1009
+ #else
1010
+ silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth));
1011
+ #endif
1012
+
1013
+ return silence;
1014
+ }
1015
+
1016
+ #ifdef FIXED_POINT
1017
+ static opus_val32 compute_frame_energy(const opus_res *pcm, int frame_size, int channels, int arch)
1018
+ {
1019
+ int i;
1020
+ opus_val32 sample_max;
1021
+ int max_shift;
1022
+ int shift;
1023
+ opus_val32 energy = 0;
1024
+ int len = frame_size*channels;
1025
+ (void)arch;
1026
+ /* Max amplitude in the signal */
1027
+ sample_max = RES2INT16(celt_maxabs_res(pcm, len));
1028
+
1029
+ /* Compute the right shift required in the MAC to avoid an overflow */
1030
+ max_shift = celt_ilog2(len);
1031
+ shift = IMAX(0, (celt_ilog2(1+sample_max) << 1) + max_shift - 28);
1032
+
1033
+ /* Compute the energy */
1034
+ for (i=0; i<len; i++)
1035
+ energy += SHR32(MULT16_16(RES2INT16(pcm[i]), RES2INT16(pcm[i])), shift);
1036
+
1037
+ /* Normalize energy by the frame size and left-shift back to the original position */
1038
+ energy /= len;
1039
+ energy = SHL32(energy, shift);
1040
+
1041
+ return energy;
1042
+ }
1043
+ #else
1044
+ static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
1045
+ {
1046
+ int len = frame_size*channels;
1047
+ return celt_inner_prod(pcm, pcm, len, arch)/len;
1048
+ }
1049
+ #endif
1050
+
1051
+ /* Decides if DTX should be turned on (=1) or off (=0) */
1052
+ static int decide_dtx_mode(opus_int activity, /* indicates if this frame contains speech/music */
1053
+ int *nb_no_activity_ms_Q1, /* number of consecutive milliseconds with no activity, in Q1 */
1054
+ int frame_size_ms_Q1 /* number of miliseconds in this update, in Q1 */
1055
+ )
1056
+
1057
+ {
1058
+ if (!activity)
1059
+ {
1060
+ /* The number of consecutive DTX frames should be within the allowed bounds.
1061
+ Note that the allowed bound is defined in the SILK headers and assumes 20 ms
1062
+ frames. As this function can be called with any frame length, a conversion to
1063
+ milliseconds is done before the comparisons. */
1064
+ (*nb_no_activity_ms_Q1) += frame_size_ms_Q1;
1065
+ if (*nb_no_activity_ms_Q1 > NB_SPEECH_FRAMES_BEFORE_DTX*20*2)
1066
+ {
1067
+ if (*nb_no_activity_ms_Q1 <= (NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX)*20*2)
1068
+ /* Valid frame for DTX! */
1069
+ return 1;
1070
+ else
1071
+ (*nb_no_activity_ms_Q1) = NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
1072
+ }
1073
+ } else
1074
+ (*nb_no_activity_ms_Q1) = 0;
1075
+
1076
+ return 0;
1077
+ }
1078
+
1079
+ #endif
1080
+
1081
+ static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels)
1082
+ {
1083
+ int redundancy_bytes_cap;
1084
+ int redundancy_bytes;
1085
+ opus_int32 redundancy_rate;
1086
+ int base_bits;
1087
+ opus_int32 available_bits;
1088
+ base_bits = (40*channels+20);
1089
+
1090
+ /* Equivalent rate for 5 ms frames. */
1091
+ redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate);
1092
+ /* For VBR, further increase the bitrate if we can afford it. It's pretty short
1093
+ and we'll avoid artefacts. */
1094
+ redundancy_rate = 3*redundancy_rate/2;
1095
+ redundancy_bytes = redundancy_rate/1600;
1096
+
1097
+ /* Compute the max rate we can use given CBR or VBR with cap. */
1098
+ available_bits = max_data_bytes*8 - 2*base_bits;
1099
+ redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8;
1100
+ redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap);
1101
+ /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */
1102
+ if (redundancy_bytes > 4 + 8*channels)
1103
+ redundancy_bytes = IMIN(257, redundancy_bytes);
1104
+ else
1105
+ redundancy_bytes = 0;
1106
+ return redundancy_bytes;
1107
+ }
1108
+
1109
+ static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1110
+ unsigned char *data, opus_int32 max_data_bytes,
1111
+ int float_api, int first_frame,
1112
+ #ifdef ENABLE_DRED
1113
+ opus_int32 dred_bitrate_bps,
1114
+ #endif
1115
+ #ifndef DISABLE_FLOAT_API
1116
+ AnalysisInfo *analysis_info, int is_silence,
1117
+ #endif
1118
+ int redundancy, int celt_to_silk, int prefill,
1119
+ opus_int32 equiv_rate, int to_celt);
1120
+
1121
+ opus_int32 opus_encode_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1122
+ unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
1123
+ const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
1124
+ int analysis_channels, downmix_func downmix, int float_api)
1125
+ {
1126
+ void *silk_enc;
1127
+ CELTEncoder *celt_enc;
1128
+ int i;
1129
+ int ret=0;
1130
+ int prefill=0;
1131
+ int redundancy = 0;
1132
+ int celt_to_silk = 0;
1133
+ int to_celt = 0;
1134
+ int voice_est; /* Probability of voice in Q7 */
1135
+ opus_int32 equiv_rate;
1136
+ int frame_rate;
1137
+ opus_int32 max_rate; /* Max bitrate we're allowed to use */
1138
+ int curr_bandwidth;
1139
+ opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1140
+ opus_int32 cbr_bytes=-1;
1141
+ opus_val16 stereo_width;
1142
+ const CELTMode *celt_mode;
1143
+ #ifndef DISABLE_FLOAT_API
1144
+ AnalysisInfo analysis_info;
1145
+ int analysis_read_pos_bak=-1;
1146
+ int analysis_read_subframe_bak=-1;
1147
+ int is_silence = 0;
1148
+ #endif
1149
+ #ifdef ENABLE_DRED
1150
+ opus_int32 dred_bitrate_bps;
1151
+ #endif
1152
+ ALLOC_STACK;
1153
+
1154
+ max_data_bytes = IMIN(1276, out_data_bytes);
1155
+
1156
+ st->rangeFinal = 0;
1157
+ if (frame_size <= 0 || max_data_bytes <= 0)
1158
+ {
1159
+ RESTORE_STACK;
1160
+ return OPUS_BAD_ARG;
1161
+ }
1162
+
1163
+ /* Cannot encode 100 ms in 1 byte */
1164
+ if (max_data_bytes==1 && st->Fs==(frame_size*10))
1165
+ {
1166
+ RESTORE_STACK;
1167
+ return OPUS_BUFFER_TOO_SMALL;
1168
+ }
1169
+
1170
+ silk_enc = (char*)st+st->silk_enc_offset;
1171
+ celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1172
+
1173
+ lsb_depth = IMIN(lsb_depth, st->lsb_depth);
1174
+
1175
+ celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1176
+ #ifndef DISABLE_FLOAT_API
1177
+ analysis_info.valid = 0;
1178
+ #ifdef FIXED_POINT
1179
+ if (st->silk_mode.complexity >= 10 && st->Fs>=16000)
1180
+ #else
1181
+ if (st->silk_mode.complexity >= 7 && st->Fs>=16000)
1182
+ #endif
1183
+ {
1184
+ is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1185
+ analysis_read_pos_bak = st->analysis.read_pos;
1186
+ analysis_read_subframe_bak = st->analysis.read_subframe;
1187
+ run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1188
+ c1, c2, analysis_channels, st->Fs,
1189
+ lsb_depth, downmix, &analysis_info);
1190
+
1191
+ /* Track the peak signal energy */
1192
+ if (!is_silence && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD)
1193
+ st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),
1194
+ compute_frame_energy(pcm, frame_size, st->channels, st->arch));
1195
+ } else if (st->analysis.initialized) {
1196
+ tonality_analysis_reset(&st->analysis);
1197
+ }
1198
+ #else
1199
+ (void)analysis_pcm;
1200
+ (void)analysis_size;
1201
+ (void)c1;
1202
+ (void)c2;
1203
+ (void)analysis_channels;
1204
+ (void)downmix;
1205
+ #endif
1206
+
1207
+ #ifndef DISABLE_FLOAT_API
1208
+ /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1209
+ * Otherwise, preserve voice_ratio from the last non-silent frame */
1210
+ if (!is_silence)
1211
+ st->voice_ratio = -1;
1212
+
1213
+ st->detected_bandwidth = 0;
1214
+ if (analysis_info.valid)
1215
+ {
1216
+ int analysis_bandwidth;
1217
+ if (st->signal_type == OPUS_AUTO)
1218
+ {
1219
+ float prob;
1220
+ if (st->prev_mode == 0)
1221
+ prob = analysis_info.music_prob;
1222
+ else if (st->prev_mode == MODE_CELT_ONLY)
1223
+ prob = analysis_info.music_prob_max;
1224
+ else
1225
+ prob = analysis_info.music_prob_min;
1226
+ st->voice_ratio = (int)floor(.5+100*(1-prob));
1227
+ }
1228
+
1229
+ analysis_bandwidth = analysis_info.bandwidth;
1230
+ if (analysis_bandwidth<=12)
1231
+ st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1232
+ else if (analysis_bandwidth<=14)
1233
+ st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1234
+ else if (analysis_bandwidth<=16)
1235
+ st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1236
+ else if (analysis_bandwidth<=18)
1237
+ st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1238
+ else
1239
+ st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1240
+ }
1241
+ #else
1242
+ st->voice_ratio = -1;
1243
+ #endif
1244
+
1245
+ if (st->channels==2 && st->force_channels!=1)
1246
+ stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1247
+ else
1248
+ stereo_width = 0;
1249
+ st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1250
+
1251
+ frame_rate = st->Fs/frame_size;
1252
+ if (!st->use_vbr)
1253
+ {
1254
+ /* Multiply by 12 to make sure the division is exact. */
1255
+ int frame_rate12 = 12*st->Fs/frame_size;
1256
+ /* We need to make sure that "int" values always fit in 16 bits. */
1257
+ cbr_bytes = IMIN( (12*st->bitrate_bps/8 + frame_rate12/2)/frame_rate12, max_data_bytes);
1258
+ st->bitrate_bps = cbr_bytes*(opus_int32)frame_rate12*8/12;
1259
+ /* Make sure we provide at least one byte to avoid failing. */
1260
+ max_data_bytes = IMAX(1, cbr_bytes);
1261
+ }
1262
+ #ifdef ENABLE_DRED
1263
+ /* Allocate some of the bits to DRED if needed. */
1264
+ dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1265
+ st->bitrate_bps -= dred_bitrate_bps;
1266
+ #endif
1267
+ if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1268
+ || (frame_rate<50 && (max_data_bytes*frame_rate<300 || st->bitrate_bps < 2400)))
1269
+ {
1270
+ /*If the space is too low to do something useful, emit 'PLC' frames.*/
1271
+ int tocmode = st->mode;
1272
+ int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND : st->bandwidth;
1273
+ int packet_code = 0;
1274
+ int num_multiframes = 0;
1275
+
1276
+ if (tocmode==0)
1277
+ tocmode = MODE_SILK_ONLY;
1278
+ if (frame_rate>100)
1279
+ tocmode = MODE_CELT_ONLY;
1280
+ /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1281
+ if (frame_rate==25 && tocmode!=MODE_SILK_ONLY)
1282
+ {
1283
+ frame_rate = 50;
1284
+ packet_code = 1;
1285
+ }
1286
+
1287
+ /* >= 60 ms frames */
1288
+ if (frame_rate<=16)
1289
+ {
1290
+ /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1291
+ if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY && frame_rate!=10))
1292
+ {
1293
+ tocmode = MODE_SILK_ONLY;
1294
+
1295
+ packet_code = frame_rate <= 12;
1296
+ frame_rate = frame_rate == 12 ? 25 : 16;
1297
+ }
1298
+ else
1299
+ {
1300
+ num_multiframes = 50/frame_rate;
1301
+ frame_rate = 50;
1302
+ packet_code = 3;
1303
+ }
1304
+ }
1305
+
1306
+ if(tocmode==MODE_SILK_ONLY&&bw>OPUS_BANDWIDTH_WIDEBAND)
1307
+ bw=OPUS_BANDWIDTH_WIDEBAND;
1308
+ else if (tocmode==MODE_CELT_ONLY&&bw==OPUS_BANDWIDTH_MEDIUMBAND)
1309
+ bw=OPUS_BANDWIDTH_NARROWBAND;
1310
+ else if (tocmode==MODE_HYBRID&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND)
1311
+ bw=OPUS_BANDWIDTH_SUPERWIDEBAND;
1312
+
1313
+ data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1314
+ data[0] |= packet_code;
1315
+
1316
+ ret = packet_code <= 1 ? 1 : 2;
1317
+
1318
+ max_data_bytes = IMAX(max_data_bytes, ret);
1319
+
1320
+ if (packet_code==3)
1321
+ data[1] = num_multiframes;
1322
+
1323
+ if (!st->use_vbr)
1324
+ {
1325
+ ret = opus_packet_pad(data, ret, max_data_bytes);
1326
+ if (ret == OPUS_OK)
1327
+ ret = max_data_bytes;
1328
+ else
1329
+ ret = OPUS_INTERNAL_ERROR;
1330
+ }
1331
+ RESTORE_STACK;
1332
+ return ret;
1333
+ }
1334
+ max_rate = frame_rate*max_data_bytes*8;
1335
+
1336
+ /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1337
+ equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1338
+ st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1339
+
1340
+ if (st->signal_type == OPUS_SIGNAL_VOICE)
1341
+ voice_est = 127;
1342
+ else if (st->signal_type == OPUS_SIGNAL_MUSIC)
1343
+ voice_est = 0;
1344
+ else if (st->voice_ratio >= 0)
1345
+ {
1346
+ voice_est = st->voice_ratio*327>>8;
1347
+ /* For AUDIO, never be more than 90% confident of having speech */
1348
+ if (st->application == OPUS_APPLICATION_AUDIO)
1349
+ voice_est = IMIN(voice_est, 115);
1350
+ } else if (st->application == OPUS_APPLICATION_VOIP)
1351
+ voice_est = 115;
1352
+ else
1353
+ voice_est = 48;
1354
+
1355
+ if (st->force_channels!=OPUS_AUTO && st->channels == 2)
1356
+ {
1357
+ st->stream_channels = st->force_channels;
1358
+ } else {
1359
+ #ifdef FUZZING
1360
+ (void)stereo_music_threshold;
1361
+ (void)stereo_voice_threshold;
1362
+ /* Random mono/stereo decision */
1363
+ if (st->channels == 2 && (rand()&0x1F)==0)
1364
+ st->stream_channels = 3-st->stream_channels;
1365
+ #else
1366
+ /* Rate-dependent mono-stereo decision */
1367
+ if (st->channels == 2)
1368
+ {
1369
+ opus_int32 stereo_threshold;
1370
+ stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1371
+ if (st->stream_channels == 2)
1372
+ stereo_threshold -= 1000;
1373
+ else
1374
+ stereo_threshold += 1000;
1375
+ st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1376
+ } else {
1377
+ st->stream_channels = st->channels;
1378
+ }
1379
+ #endif
1380
+ }
1381
+ /* Update equivalent rate for channels decision. */
1382
+ equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1383
+ st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1384
+
1385
+ /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1386
+ e.g. because of the complexity setting or sample rate. */
1387
+ #ifndef DISABLE_FLOAT_API
1388
+ st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1389
+ #else
1390
+ st->silk_mode.useDTX = st->use_dtx;
1391
+ #endif
1392
+
1393
+ /* Mode selection depending on application and signal type */
1394
+ if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1395
+ {
1396
+ st->mode = MODE_CELT_ONLY;
1397
+ } else if (st->user_forced_mode == OPUS_AUTO)
1398
+ {
1399
+ #ifdef FUZZING
1400
+ (void)stereo_width;
1401
+ (void)mode_thresholds;
1402
+ /* Random mode switching */
1403
+ if ((rand()&0xF)==0)
1404
+ {
1405
+ if ((rand()&0x1)==0)
1406
+ st->mode = MODE_CELT_ONLY;
1407
+ else
1408
+ st->mode = MODE_SILK_ONLY;
1409
+ } else {
1410
+ if (st->prev_mode==MODE_CELT_ONLY)
1411
+ st->mode = MODE_CELT_ONLY;
1412
+ else
1413
+ st->mode = MODE_SILK_ONLY;
1414
+ }
1415
+ #else
1416
+ opus_int32 mode_voice, mode_music;
1417
+ opus_int32 threshold;
1418
+
1419
+ /* Interpolate based on stereo width */
1420
+ mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])
1421
+ + MULT16_32_Q15(stereo_width,mode_thresholds[1][0]));
1422
+ mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])
1423
+ + MULT16_32_Q15(stereo_width,mode_thresholds[1][1]));
1424
+ /* Interpolate based on speech/music probability */
1425
+ threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1426
+ /* Bias towards SILK for VoIP because of some useful features */
1427
+ if (st->application == OPUS_APPLICATION_VOIP)
1428
+ threshold += 8000;
1429
+
1430
+ /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1431
+ /* Hysteresis */
1432
+ if (st->prev_mode == MODE_CELT_ONLY)
1433
+ threshold -= 4000;
1434
+ else if (st->prev_mode>0)
1435
+ threshold += 4000;
1436
+
1437
+ st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY: MODE_SILK_ONLY;
1438
+
1439
+ /* When FEC is enabled and there's enough packet loss, use SILK.
1440
+ Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1441
+ if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1442
+ st->mode = MODE_SILK_ONLY;
1443
+ /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1444
+ use SILK in order to make use of its DTX. */
1445
+ if (st->silk_mode.useDTX && voice_est > 100)
1446
+ st->mode = MODE_SILK_ONLY;
1447
+ #endif
1448
+
1449
+ /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1450
+ if (max_data_bytes < (frame_rate > 50 ? 9000 : 6000)*frame_size / (st->Fs * 8))
1451
+ st->mode = MODE_CELT_ONLY;
1452
+ } else {
1453
+ st->mode = st->user_forced_mode;
1454
+ }
1455
+
1456
+ /* Override the chosen mode to make sure we meet the requested frame size */
1457
+ if (st->mode != MODE_CELT_ONLY && frame_size < st->Fs/100)
1458
+ st->mode = MODE_CELT_ONLY;
1459
+ if (st->lfe)
1460
+ st->mode = MODE_CELT_ONLY;
1461
+
1462
+ if (st->prev_mode > 0 &&
1463
+ ((st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) ||
1464
+ (st->mode == MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)))
1465
+ {
1466
+ redundancy = 1;
1467
+ celt_to_silk = (st->mode != MODE_CELT_ONLY);
1468
+ if (!celt_to_silk)
1469
+ {
1470
+ /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1471
+ if (frame_size >= st->Fs/100)
1472
+ {
1473
+ st->mode = st->prev_mode;
1474
+ to_celt = 1;
1475
+ } else {
1476
+ redundancy=0;
1477
+ }
1478
+ }
1479
+ }
1480
+
1481
+ /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1482
+ * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1483
+ if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1484
+ && st->mode != MODE_CELT_ONLY && st->prev_mode != MODE_CELT_ONLY)
1485
+ {
1486
+ /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1487
+ st->silk_mode.toMono = 1;
1488
+ st->stream_channels = 2;
1489
+ } else {
1490
+ st->silk_mode.toMono = 0;
1491
+ }
1492
+
1493
+ /* Update equivalent rate with mode decision. */
1494
+ equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1495
+ st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1496
+
1497
+ if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
1498
+ {
1499
+ silk_EncControlStruct dummy;
1500
+ silk_InitEncoder( silk_enc, st->arch, &dummy);
1501
+ prefill=1;
1502
+ }
1503
+
1504
+ /* Automatic (rate-dependent) bandwidth selection */
1505
+ if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthSwitch)
1506
+ {
1507
+ const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1508
+ opus_int32 bandwidth_thresholds[8];
1509
+ int bandwidth = OPUS_BANDWIDTH_FULLBAND;
1510
+
1511
+ if (st->channels==2 && st->force_channels!=1)
1512
+ {
1513
+ voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1514
+ music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1515
+ } else {
1516
+ voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1517
+ music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1518
+ }
1519
+ /* Interpolate bandwidth thresholds depending on voice estimation */
1520
+ for (i=0;i<8;i++)
1521
+ {
1522
+ bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1523
+ + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1524
+ }
1525
+ do {
1526
+ int threshold, hysteresis;
1527
+ threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)];
1528
+ hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND)+1];
1529
+ if (!st->first)
1530
+ {
1531
+ if (st->auto_bandwidth >= bandwidth)
1532
+ threshold -= hysteresis;
1533
+ else
1534
+ threshold += hysteresis;
1535
+ }
1536
+ if (equiv_rate >= threshold)
1537
+ break;
1538
+ } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND);
1539
+ /* We don't use mediumband anymore, except when explicitly requested or during
1540
+ mode transitions. */
1541
+ if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1542
+ bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1543
+ st->bandwidth = st->auto_bandwidth = bandwidth;
1544
+ /* Prevents any transition to SWB/FB until the SILK layer has fully
1545
+ switched to WB mode and turned the variable LP filter off */
1546
+ if (!st->first && st->mode != MODE_CELT_ONLY && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1547
+ st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1548
+ }
1549
+
1550
+ if (st->bandwidth>st->max_bandwidth)
1551
+ st->bandwidth = st->max_bandwidth;
1552
+
1553
+ if (st->user_bandwidth != OPUS_AUTO)
1554
+ st->bandwidth = st->user_bandwidth;
1555
+
1556
+ /* This prevents us from using hybrid at unsafe CBR/max rates */
1557
+ if (st->mode != MODE_CELT_ONLY && max_rate < 15000)
1558
+ {
1559
+ st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND);
1560
+ }
1561
+
1562
+ /* Prevents Opus from wasting bits on frequencies that are above
1563
+ the Nyquist rate of the input signal */
1564
+ if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND)
1565
+ st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1566
+ if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1567
+ st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1568
+ if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND)
1569
+ st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1570
+ if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND)
1571
+ st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1572
+ #ifndef DISABLE_FLOAT_API
1573
+ /* Use detected bandwidth to reduce the encoded bandwidth. */
1574
+ if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO)
1575
+ {
1576
+ int min_detected_bandwidth;
1577
+ /* Makes bandwidth detection more conservative just in case the detector
1578
+ gets it wrong when we could have coded a high bandwidth transparently.
1579
+ When operating in SILK/hybrid mode, we don't go below wideband to avoid
1580
+ more complicated switches that require redundancy. */
1581
+ if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1582
+ min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1583
+ else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY)
1584
+ min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1585
+ else if (equiv_rate <= 30000*st->stream_channels)
1586
+ min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1587
+ else if (equiv_rate <= 44000*st->stream_channels)
1588
+ min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1589
+ else
1590
+ min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1591
+
1592
+ st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth);
1593
+ st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth);
1594
+ }
1595
+ #endif
1596
+ st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1597
+ st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1598
+ celt_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth));
1599
+
1600
+ /* CELT mode doesn't support mediumband, use wideband instead */
1601
+ if (st->mode == MODE_CELT_ONLY && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1602
+ st->bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1603
+ if (st->lfe)
1604
+ st->bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1605
+
1606
+ curr_bandwidth = st->bandwidth;
1607
+
1608
+ /* Chooses the appropriate mode for speech
1609
+ *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1610
+ if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND)
1611
+ st->mode = MODE_HYBRID;
1612
+ if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND)
1613
+ st->mode = MODE_SILK_ONLY;
1614
+
1615
+ /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1616
+ if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY)) || frame_size > 3*st->Fs/50)
1617
+ {
1618
+ int enc_frame_size;
1619
+ int nb_frames;
1620
+ VARDECL(unsigned char, tmp_data);
1621
+ VARDECL(OpusRepacketizer, rp);
1622
+ int max_header_bytes;
1623
+ opus_int32 repacketize_len;
1624
+ opus_int32 max_len_sum;
1625
+ opus_int32 tot_size=0;
1626
+ unsigned char *curr_data;
1627
+ int tmp_len;
1628
+ int dtx_count = 0;
1629
+ int bak_to_mono;
1630
+
1631
+ if (st->mode == MODE_SILK_ONLY)
1632
+ {
1633
+ if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */
1634
+ enc_frame_size = st->Fs/25;
1635
+ else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */
1636
+ enc_frame_size = 3*st->Fs/50;
1637
+ else /* 100 ms -> 5x 20 ms */
1638
+ enc_frame_size = st->Fs/50;
1639
+ }
1640
+ else
1641
+ enc_frame_size = st->Fs/50;
1642
+
1643
+ nb_frames = frame_size/enc_frame_size;
1644
+
1645
+ #ifndef DISABLE_FLOAT_API
1646
+ if (analysis_read_pos_bak!= -1)
1647
+ {
1648
+ /* Reset analysis position to the beginning of the first frame so we
1649
+ can use it one frame at a time. */
1650
+ st->analysis.read_pos = analysis_read_pos_bak;
1651
+ st->analysis.read_subframe = analysis_read_subframe_bak;
1652
+ }
1653
+ #endif
1654
+
1655
+ /* Worst cases:
1656
+ * 2 frames: Code 2 with different compressed sizes
1657
+ * >2 frames: Code 3 VBR */
1658
+ max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1659
+
1660
+ if (st->use_vbr || st->user_bitrate_bps==OPUS_BITRATE_MAX)
1661
+ repacketize_len = out_data_bytes;
1662
+ else {
1663
+ celt_assert(cbr_bytes>=0);
1664
+ repacketize_len = IMIN(cbr_bytes, out_data_bytes);
1665
+ }
1666
+ max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1667
+
1668
+ ALLOC(tmp_data, max_len_sum, unsigned char);
1669
+ curr_data = tmp_data;
1670
+ ALLOC(rp, 1, OpusRepacketizer);
1671
+ opus_repacketizer_init(rp);
1672
+
1673
+
1674
+ bak_to_mono = st->silk_mode.toMono;
1675
+ if (bak_to_mono)
1676
+ st->force_channels = 1;
1677
+ else
1678
+ st->prev_channels = st->stream_channels;
1679
+
1680
+ for (i=0;i<nb_frames;i++)
1681
+ {
1682
+ int first_frame;
1683
+ int frame_to_celt;
1684
+ int frame_redundancy;
1685
+ opus_int32 curr_max;
1686
+ /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1687
+ that allows for DRED not to be in the first frame. */
1688
+ first_frame = (i == 0) || (i == dtx_count);
1689
+ st->silk_mode.toMono = 0;
1690
+ st->nonfinal_frame = i<(nb_frames-1);
1691
+
1692
+ /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1693
+ frame_to_celt = to_celt && i==nb_frames-1;
1694
+ frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1695
+
1696
+ curr_max = IMIN(3*st->bitrate_bps/(3*8*st->Fs/enc_frame_size), max_len_sum/nb_frames);
1697
+ #ifdef ENABLE_DRED
1698
+ curr_max = IMIN(curr_max, (max_len_sum-3*dred_bitrate_bps/(3*8*st->Fs/frame_size))/nb_frames);
1699
+ if (first_frame) curr_max += 3*dred_bitrate_bps/(3*8*st->Fs/frame_size);
1700
+ #endif
1701
+ curr_max = IMIN(max_len_sum-tot_size, curr_max);
1702
+ #ifndef DISABLE_FLOAT_API
1703
+ if (analysis_read_pos_bak != -1) {
1704
+ is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1705
+ /* Get analysis for current frame. */
1706
+ tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1707
+ }
1708
+ #endif
1709
+
1710
+ tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1711
+ #ifdef ENABLE_DRED
1712
+ dred_bitrate_bps,
1713
+ #endif
1714
+ #ifndef DISABLE_FLOAT_API
1715
+ &analysis_info,
1716
+ is_silence,
1717
+ #endif
1718
+ frame_redundancy, celt_to_silk, prefill,
1719
+ equiv_rate, frame_to_celt
1720
+ );
1721
+ if (tmp_len<0)
1722
+ {
1723
+ RESTORE_STACK;
1724
+ return OPUS_INTERNAL_ERROR;
1725
+ } else if (tmp_len==1) {
1726
+ dtx_count++;
1727
+ }
1728
+ ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1729
+
1730
+ if (ret<0)
1731
+ {
1732
+ RESTORE_STACK;
1733
+ return OPUS_INTERNAL_ERROR;
1734
+ }
1735
+ tot_size += tmp_len;
1736
+ curr_data += tmp_len;
1737
+ }
1738
+ ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL, 0);
1739
+ if (ret<0)
1740
+ {
1741
+ ret = OPUS_INTERNAL_ERROR;
1742
+ }
1743
+ st->silk_mode.toMono = bak_to_mono;
1744
+ RESTORE_STACK;
1745
+ return ret;
1746
+ } else {
1747
+ ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1748
+ #ifdef ENABLE_DRED
1749
+ dred_bitrate_bps,
1750
+ #endif
1751
+ #ifndef DISABLE_FLOAT_API
1752
+ &analysis_info,
1753
+ is_silence,
1754
+ #endif
1755
+ redundancy, celt_to_silk, prefill,
1756
+ equiv_rate, to_celt
1757
+ );
1758
+ RESTORE_STACK;
1759
+ return ret;
1760
+ }
1761
+ }
1762
+
1763
+ static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1764
+ unsigned char *data, opus_int32 max_data_bytes,
1765
+ int float_api, int first_frame,
1766
+ #ifdef ENABLE_DRED
1767
+ opus_int32 dred_bitrate_bps,
1768
+ #endif
1769
+ #ifndef DISABLE_FLOAT_API
1770
+ AnalysisInfo *analysis_info, int is_silence,
1771
+ #endif
1772
+ int redundancy, int celt_to_silk, int prefill,
1773
+ opus_int32 equiv_rate, int to_celt)
1774
+ {
1775
+ void *silk_enc;
1776
+ CELTEncoder *celt_enc;
1777
+ const CELTMode *celt_mode;
1778
+ int i;
1779
+ int ret=0;
1780
+ opus_int32 nBytes;
1781
+ ec_enc enc;
1782
+ int bytes_target;
1783
+ int start_band = 0;
1784
+ int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1785
+ int nb_compr_bytes;
1786
+ opus_uint32 redundant_rng = 0;
1787
+ int cutoff_Hz;
1788
+ int hp_freq_smth1;
1789
+ opus_val16 HB_gain;
1790
+ int apply_padding;
1791
+ int frame_rate;
1792
+ int curr_bandwidth;
1793
+ int delay_compensation;
1794
+ int total_buffer;
1795
+ opus_int activity = VAD_NO_DECISION;
1796
+ VARDECL(opus_res, pcm_buf);
1797
+ VARDECL(opus_res, tmp_prefill);
1798
+ SAVE_STACK;
1799
+
1800
+ st->rangeFinal = 0;
1801
+ silk_enc = (char*)st+st->silk_enc_offset;
1802
+ celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
1803
+ celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
1804
+ curr_bandwidth = st->bandwidth;
1805
+ if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1806
+ delay_compensation = 0;
1807
+ else
1808
+ delay_compensation = st->delay_compensation;
1809
+ total_buffer = delay_compensation;
1810
+
1811
+ frame_rate = st->Fs/frame_size;
1812
+
1813
+ #ifndef DISABLE_FLOAT_API
1814
+ if (is_silence)
1815
+ {
1816
+ activity = !is_silence;
1817
+ } else if (analysis_info->valid)
1818
+ {
1819
+ activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD;
1820
+ if (!activity)
1821
+ {
1822
+ /* Mark as active if this noise frame is sufficiently loud */
1823
+ opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1824
+ activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD * noise_energy);
1825
+ }
1826
+ }
1827
+ #endif
1828
+
1829
+ /* For the first frame at a new SILK bandwidth */
1830
+ if (st->silk_bw_switch)
1831
+ {
1832
+ redundancy = 1;
1833
+ celt_to_silk = 1;
1834
+ st->silk_bw_switch = 0;
1835
+ /* Do a prefill without resetting the sampling rate control. */
1836
+ prefill=2;
1837
+ }
1838
+
1839
+ /* If we decided to go with CELT, make sure redundancy is off, no matter what
1840
+ we decided earlier. */
1841
+ if (st->mode == MODE_CELT_ONLY)
1842
+ redundancy = 0;
1843
+
1844
+ if (redundancy)
1845
+ {
1846
+ redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1847
+ if (redundancy_bytes == 0)
1848
+ redundancy = 0;
1849
+ }
1850
+
1851
+ /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1852
+ bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame_size / (st->Fs * 8)) - 1;
1853
+
1854
+ data += 1;
1855
+
1856
+ ec_enc_init(&enc, data, max_data_bytes-1);
1857
+
1858
+ ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res);
1859
+ OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels);
1860
+
1861
+ if (st->mode == MODE_CELT_ONLY)
1862
+ hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1863
+ else
1864
+ hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1865
+
1866
+ st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1867
+ hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) );
1868
+
1869
+ /* convert from log scale to Hertz */
1870
+ cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1871
+
1872
+ if (st->application == OPUS_APPLICATION_VOIP)
1873
+ {
1874
+ hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
1875
+
1876
+ #ifdef ENABLE_OSCE_TRAINING_DATA
1877
+ /* write out high pass filtered clean signal*/
1878
+ static FILE *fout =NULL;
1879
+ if (fout == NULL)
1880
+ {
1881
+ fout = fopen("clean_hp.s16", "wb");
1882
+ }
1883
+
1884
+ {
1885
+ int idx;
1886
+ opus_int16 tmp;
1887
+ for (idx = 0; idx < frame_size; idx++)
1888
+ {
1889
+ tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
1890
+ fwrite(&tmp, sizeof(tmp), 1, fout);
1891
+ }
1892
+ }
1893
+ #endif
1894
+ } else {
1895
+ dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
1896
+ }
1897
+ #ifndef FIXED_POINT
1898
+ if (float_api)
1899
+ {
1900
+ opus_val32 sum;
1901
+ sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch);
1902
+ /* This should filter out both NaNs and ridiculous signals that could
1903
+ cause NaNs further down. */
1904
+ if (!(sum < 1e9f) || celt_isnan(sum))
1905
+ {
1906
+ OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels);
1907
+ st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
1908
+ }
1909
+ }
1910
+ #else
1911
+ (void)float_api;
1912
+ #endif
1913
+
1914
+ #ifdef ENABLE_DRED
1915
+ if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
1916
+ int frame_size_400Hz;
1917
+ /* DRED Encoder */
1918
+ dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
1919
+ frame_size_400Hz = frame_size*400/st->Fs;
1920
+ OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz);
1921
+ for (i=0;i<frame_size_400Hz;i++)
1922
+ st->activity_mem[i] = activity;
1923
+ } else {
1924
+ st->dred_encoder.latents_buffer_fill = 0;
1925
+ OPUS_CLEAR(st->activity_mem, DRED_MAX_FRAMES);
1926
+ }
1927
+ #endif
1928
+
1929
+ /* SILK processing */
1930
+ HB_gain = Q15ONE;
1931
+ if (st->mode != MODE_CELT_ONLY)
1932
+ {
1933
+ opus_int32 total_bitRate, celt_rate;
1934
+ const opus_res *pcm_silk;
1935
+
1936
+ /* Distribute bits between SILK and CELT */
1937
+ total_bitRate = 8 * bytes_target * frame_rate;
1938
+ if( st->mode == MODE_HYBRID ) {
1939
+ /* Base rate for SILK */
1940
+ st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
1941
+ curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
1942
+ st->stream_channels);
1943
+ if (!st->energy_masking)
1944
+ {
1945
+ /* Increasingly attenuate high band when it gets allocated fewer bits */
1946
+ celt_rate = total_bitRate - st->silk_mode.bitRate;
1947
+ HB_gain = Q15ONE - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1);
1948
+ }
1949
+ } else {
1950
+ /* SILK gets all bits */
1951
+ st->silk_mode.bitRate = total_bitRate;
1952
+ }
1953
+
1954
+ /* Surround masking for SILK */
1955
+ if (st->energy_masking && st->use_vbr && !st->lfe)
1956
+ {
1957
+ opus_val32 mask_sum=0;
1958
+ celt_glog masking_depth;
1959
+ opus_int32 rate_offset;
1960
+ int c;
1961
+ int end = 17;
1962
+ opus_int16 srate = 16000;
1963
+ if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND)
1964
+ {
1965
+ end = 13;
1966
+ srate = 8000;
1967
+ } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND)
1968
+ {
1969
+ end = 15;
1970
+ srate = 12000;
1971
+ }
1972
+ for (c=0;c<st->channels;c++)
1973
+ {
1974
+ for(i=0;i<end;i++)
1975
+ {
1976
+ celt_glog mask;
1977
+ mask = MAXG(MING(st->energy_masking[21*c+i],
1978
+ GCONST(.5f)), -GCONST(2.0f));
1979
+ if (mask > 0)
1980
+ mask = HALF32(mask);
1981
+ mask_sum += mask;
1982
+ }
1983
+ }
1984
+ /* Conservative rate reduction, we cut the masking in half */
1985
+ masking_depth = mask_sum / end*st->channels;
1986
+ masking_depth += GCONST(.2f);
1987
+ rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10);
1988
+ rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3);
1989
+ /* Split the rate change between the SILK and CELT part for hybrid. */
1990
+ if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND || st->bandwidth==OPUS_BANDWIDTH_FULLBAND)
1991
+ st->silk_mode.bitRate += 3*rate_offset/5;
1992
+ else
1993
+ st->silk_mode.bitRate += rate_offset;
1994
+ }
1995
+
1996
+ st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
1997
+ st->silk_mode.nChannelsAPI = st->channels;
1998
+ st->silk_mode.nChannelsInternal = st->stream_channels;
1999
+ if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2000
+ st->silk_mode.desiredInternalSampleRate = 8000;
2001
+ } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2002
+ st->silk_mode.desiredInternalSampleRate = 12000;
2003
+ } else {
2004
+ celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND );
2005
+ st->silk_mode.desiredInternalSampleRate = 16000;
2006
+ }
2007
+ if( st->mode == MODE_HYBRID ) {
2008
+ /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2009
+ st->silk_mode.minInternalSampleRate = 16000;
2010
+ } else {
2011
+ st->silk_mode.minInternalSampleRate = 8000;
2012
+ }
2013
+
2014
+ st->silk_mode.maxInternalSampleRate = 16000;
2015
+ if (st->mode == MODE_SILK_ONLY)
2016
+ {
2017
+ opus_int32 effective_max_rate = frame_rate*max_data_bytes*8;
2018
+ if (frame_rate > 50)
2019
+ effective_max_rate = effective_max_rate*2/3;
2020
+ if (effective_max_rate < 8000)
2021
+ {
2022
+ st->silk_mode.maxInternalSampleRate = 12000;
2023
+ st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate);
2024
+ }
2025
+ if (effective_max_rate < 7000)
2026
+ {
2027
+ st->silk_mode.maxInternalSampleRate = 8000;
2028
+ st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate);
2029
+ }
2030
+ }
2031
+
2032
+ st->silk_mode.useCBR = !st->use_vbr;
2033
+
2034
+ /* Call SILK encoder for the low band */
2035
+
2036
+ /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2037
+ st->silk_mode.maxBits = (max_data_bytes-1)*8;
2038
+ if (redundancy && redundancy_bytes >= 2)
2039
+ {
2040
+ /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2041
+ st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2042
+ if (st->mode == MODE_HYBRID)
2043
+ st->silk_mode.maxBits -= 20;
2044
+ }
2045
+ if (st->silk_mode.useCBR)
2046
+ {
2047
+ /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2048
+ save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2049
+ produce a constant bitrate without wasting bits. */
2050
+ #ifdef ENABLE_DRED
2051
+ if (st->mode == MODE_HYBRID || dred_bitrate_bps > 0)
2052
+ #else
2053
+ if (st->mode == MODE_HYBRID)
2054
+ #endif
2055
+ {
2056
+ /* Allow SILK to steal up to 25% of the remaining bits */
2057
+ opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs);
2058
+ st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4);
2059
+ st->silk_mode.useCBR = 0;
2060
+ }
2061
+ } else {
2062
+ /* Constrained VBR. */
2063
+ if (st->mode == MODE_HYBRID)
2064
+ {
2065
+ /* Compute SILK bitrate corresponding to the max total bits available */
2066
+ opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2067
+ curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2068
+ st->stream_channels);
2069
+ st->silk_mode.maxBits = maxBitRate * frame_size / st->Fs;
2070
+ }
2071
+ }
2072
+
2073
+ if (prefill)
2074
+ {
2075
+ opus_int32 zero=0;
2076
+ int prefill_offset;
2077
+ /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2078
+ a discontinuity. The exact location is what we need to avoid leaving any "gap"
2079
+ in the audio when mixing with the redundant CELT frame. Here we can afford to
2080
+ overwrite st->delay_buffer because the only thing that uses it before it gets
2081
+ rewritten is tmp_prefill[] and even then only the part after the ramp really
2082
+ gets used (rather than sent to the encoder and discarded) */
2083
+ prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2084
+ gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2085
+ 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2086
+ OPUS_CLEAR(st->delay_buffer, prefill_offset);
2087
+ pcm_silk = st->delay_buffer;
2088
+ silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, prefill, activity );
2089
+ /* Prevent a second switch in the real encode call. */
2090
+ st->silk_mode.opusCanSwitch = 0;
2091
+ }
2092
+
2093
+ pcm_silk = pcm_buf+total_buffer*st->channels;
2094
+ ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2095
+ if( ret ) {
2096
+ /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2097
+ /* Handle error */
2098
+ RESTORE_STACK;
2099
+ return OPUS_INTERNAL_ERROR;
2100
+ }
2101
+
2102
+ /* Extract SILK internal bandwidth for signaling in first byte */
2103
+ if( st->mode == MODE_SILK_ONLY ) {
2104
+ if( st->silk_mode.internalSampleRate == 8000 ) {
2105
+ curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2106
+ } else if( st->silk_mode.internalSampleRate == 12000 ) {
2107
+ curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2108
+ } else if( st->silk_mode.internalSampleRate == 16000 ) {
2109
+ curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2110
+ }
2111
+ } else {
2112
+ celt_assert( st->silk_mode.internalSampleRate == 16000 );
2113
+ }
2114
+
2115
+ st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2116
+
2117
+ if (nBytes==0)
2118
+ {
2119
+ st->rangeFinal = 0;
2120
+ data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2121
+ RESTORE_STACK;
2122
+ return 1;
2123
+ }
2124
+
2125
+ /* FIXME: How do we allocate the redundancy for CBR? */
2126
+ if (st->silk_mode.opusCanSwitch)
2127
+ {
2128
+ redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2129
+ redundancy = (redundancy_bytes != 0);
2130
+ celt_to_silk = 0;
2131
+ st->silk_bw_switch = 1;
2132
+ }
2133
+ }
2134
+
2135
+ /* CELT processing */
2136
+ {
2137
+ int endband=21;
2138
+
2139
+ switch(curr_bandwidth)
2140
+ {
2141
+ case OPUS_BANDWIDTH_NARROWBAND:
2142
+ endband = 13;
2143
+ break;
2144
+ case OPUS_BANDWIDTH_MEDIUMBAND:
2145
+ case OPUS_BANDWIDTH_WIDEBAND:
2146
+ endband = 17;
2147
+ break;
2148
+ case OPUS_BANDWIDTH_SUPERWIDEBAND:
2149
+ endband = 19;
2150
+ break;
2151
+ case OPUS_BANDWIDTH_FULLBAND:
2152
+ endband = 21;
2153
+ break;
2154
+ }
2155
+ celt_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband));
2156
+ celt_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels));
2157
+ }
2158
+ celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2159
+ if (st->mode != MODE_SILK_ONLY)
2160
+ {
2161
+ opus_val32 celt_pred=2;
2162
+ /* We may still decide to disable prediction later */
2163
+ if (st->silk_mode.reducedDependency)
2164
+ celt_pred = 0;
2165
+ celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred));
2166
+ }
2167
+
2168
+ ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res);
2169
+ if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
2170
+ {
2171
+ OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400);
2172
+ }
2173
+
2174
+ if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2175
+ {
2176
+ OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer));
2177
+ OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],
2178
+ &pcm_buf[0],
2179
+ (frame_size+total_buffer)*st->channels);
2180
+ } else {
2181
+ OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels);
2182
+ }
2183
+ /* gain_fade() and stereo_fade() need to be after the buffer copying
2184
+ because we don't want any of this to affect the SILK part */
2185
+ if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
2186
+ gain_fade(pcm_buf, pcm_buf,
2187
+ st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2188
+ }
2189
+ st->prev_HB_gain = HB_gain;
2190
+ if (st->mode != MODE_HYBRID || st->stream_channels==1)
2191
+ {
2192
+ if (equiv_rate > 32000)
2193
+ st->silk_mode.stereoWidth_Q14 = 16384;
2194
+ else if (equiv_rate < 16000)
2195
+ st->silk_mode.stereoWidth_Q14 = 0;
2196
+ else
2197
+ st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2198
+ }
2199
+ if( !st->energy_masking && st->channels == 2 ) {
2200
+ /* Apply stereo width reduction (at low bitrates) */
2201
+ if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2202
+ opus_val16 g1, g2;
2203
+ g1 = st->hybrid_stereo_width_Q14;
2204
+ g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2205
+ #ifdef FIXED_POINT
2206
+ g1 = g1==16384 ? Q15ONE : SHL16(g1,1);
2207
+ g2 = g2==16384 ? Q15ONE : SHL16(g2,1);
2208
+ #else
2209
+ g1 *= (1.f/16384);
2210
+ g2 *= (1.f/16384);
2211
+ #endif
2212
+ stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2213
+ frame_size, st->channels, celt_mode->window, st->Fs);
2214
+ st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2215
+ }
2216
+ }
2217
+
2218
+ if ( st->mode != MODE_CELT_ONLY && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID) <= 8*(max_data_bytes-1))
2219
+ {
2220
+ /* For SILK mode, the redundancy is inferred from the length */
2221
+ if (st->mode == MODE_HYBRID)
2222
+ ec_enc_bit_logp(&enc, redundancy, 12);
2223
+ if (redundancy)
2224
+ {
2225
+ int max_redundancy;
2226
+ ec_enc_bit_logp(&enc, celt_to_silk, 1);
2227
+ if (st->mode == MODE_HYBRID)
2228
+ {
2229
+ /* Reserve the 8 bits needed for the redundancy length,
2230
+ and at least a few bits for CELT if possible */
2231
+ max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2232
+ }
2233
+ else
2234
+ max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2235
+ /* Target the same bit-rate for redundancy as for the rest,
2236
+ up to a max of 257 bytes */
2237
+ redundancy_bytes = IMIN(max_redundancy, redundancy_bytes);
2238
+ redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes));
2239
+ if (st->mode == MODE_HYBRID)
2240
+ ec_enc_uint(&enc, redundancy_bytes-2, 256);
2241
+ }
2242
+ } else {
2243
+ redundancy = 0;
2244
+ }
2245
+
2246
+ if (!redundancy)
2247
+ {
2248
+ st->silk_bw_switch = 0;
2249
+ redundancy_bytes = 0;
2250
+ }
2251
+ if (st->mode != MODE_CELT_ONLY)start_band=17;
2252
+
2253
+ if (st->mode == MODE_SILK_ONLY)
2254
+ {
2255
+ ret = (ec_tell(&enc)+7)>>3;
2256
+ ec_enc_done(&enc);
2257
+ nb_compr_bytes = ret;
2258
+ } else {
2259
+ nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2260
+ #ifdef ENABLE_DRED
2261
+ if (st->dred_duration > 0)
2262
+ {
2263
+ int max_celt_bytes;
2264
+ opus_int32 dred_bytes = dred_bitrate_bps/(frame_rate*8);
2265
+ /* Allow CELT to steal up to 25% of the remaining bits. */
2266
+ max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2267
+ /* But try to give CELT at least 5 bytes to prevent a mismatch with
2268
+ the redundancy signaling. */
2269
+ max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes);
2270
+ /* Subject to the original max. */
2271
+ nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes);
2272
+ }
2273
+ #endif
2274
+ ec_enc_shrink(&enc, nb_compr_bytes);
2275
+ }
2276
+
2277
+ #ifndef DISABLE_FLOAT_API
2278
+ if (redundancy || st->mode != MODE_SILK_ONLY)
2279
+ celt_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info));
2280
+ #endif
2281
+ if (st->mode == MODE_HYBRID) {
2282
+ SILKInfo info;
2283
+ info.signalType = st->silk_mode.signalType;
2284
+ info.offset = st->silk_mode.offset;
2285
+ celt_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info));
2286
+ }
2287
+
2288
+ /* 5 ms redundant frame for CELT->SILK */
2289
+ if (redundancy && celt_to_silk)
2290
+ {
2291
+ int err;
2292
+ celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2293
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2294
+ celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2295
+ err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL);
2296
+ if (err < 0)
2297
+ {
2298
+ RESTORE_STACK;
2299
+ return OPUS_INTERNAL_ERROR;
2300
+ }
2301
+ celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2302
+ celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2303
+ }
2304
+
2305
+ celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band));
2306
+
2307
+ if (st->mode != MODE_SILK_ONLY)
2308
+ {
2309
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr));
2310
+ if (st->mode == MODE_HYBRID)
2311
+ {
2312
+ if( st->use_vbr ) {
2313
+ celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps-st->silk_mode.bitRate));
2314
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2315
+ }
2316
+ } else {
2317
+ if (st->use_vbr)
2318
+ {
2319
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2320
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint));
2321
+ celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps));
2322
+ }
2323
+ }
2324
+ #ifdef ENABLE_DRED
2325
+ /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2326
+ if (!st->use_vbr && st->dred_duration > 0)
2327
+ {
2328
+ opus_int32 celt_bitrate = st->bitrate_bps;
2329
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1));
2330
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0));
2331
+ if (st->mode == MODE_HYBRID) {
2332
+ celt_bitrate -= st->silk_mode.bitRate;
2333
+ }
2334
+ celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate));
2335
+ }
2336
+ #endif
2337
+ if (st->mode != st->prev_mode && st->prev_mode > 0)
2338
+ {
2339
+ unsigned char dummy[2];
2340
+ celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2341
+
2342
+ /* Prefilling */
2343
+ celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL);
2344
+ celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2345
+ }
2346
+ /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2347
+ if (ec_tell(&enc) <= 8*nb_compr_bytes)
2348
+ {
2349
+ ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL, nb_compr_bytes, &enc);
2350
+ if (ret < 0)
2351
+ {
2352
+ RESTORE_STACK;
2353
+ return OPUS_INTERNAL_ERROR;
2354
+ }
2355
+ /* Put CELT->SILK redundancy data in the right place. */
2356
+ if (redundancy && celt_to_silk && st->mode==MODE_HYBRID && nb_compr_bytes != ret)
2357
+ {
2358
+ OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes);
2359
+ nb_compr_bytes = ret+redundancy_bytes;
2360
+ }
2361
+ }
2362
+ }
2363
+
2364
+ /* 5 ms redundant frame for SILK->CELT */
2365
+ if (redundancy && !celt_to_silk)
2366
+ {
2367
+ int err;
2368
+ unsigned char dummy[2];
2369
+ int N2, N4;
2370
+ N2 = st->Fs/200;
2371
+ N4 = st->Fs/400;
2372
+
2373
+ celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
2374
+ celt_encoder_ctl(celt_enc, CELT_SET_START_BAND(0));
2375
+ celt_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0));
2376
+ celt_encoder_ctl(celt_enc, OPUS_SET_VBR(0));
2377
+ celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX));
2378
+
2379
+ if (st->mode == MODE_HYBRID)
2380
+ {
2381
+ /* Shrink packet to what the encoder actually used. */
2382
+ nb_compr_bytes = ret;
2383
+ ec_enc_shrink(&enc, nb_compr_bytes);
2384
+ }
2385
+ /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2386
+ celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL);
2387
+
2388
+ err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL);
2389
+ if (err < 0)
2390
+ {
2391
+ RESTORE_STACK;
2392
+ return OPUS_INTERNAL_ERROR;
2393
+ }
2394
+ celt_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng));
2395
+ }
2396
+
2397
+
2398
+
2399
+ /* Signalling the mode in the first byte */
2400
+ data--;
2401
+ data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2402
+
2403
+ st->rangeFinal = enc.rng ^ redundant_rng;
2404
+
2405
+ if (to_celt)
2406
+ st->prev_mode = MODE_CELT_ONLY;
2407
+ else
2408
+ st->prev_mode = st->mode;
2409
+ st->prev_channels = st->stream_channels;
2410
+ st->prev_framesize = frame_size;
2411
+
2412
+ st->first = 0;
2413
+
2414
+ /* DTX decision */
2415
+ #ifndef DISABLE_FLOAT_API
2416
+ if (st->use_dtx && (analysis_info->valid || is_silence))
2417
+ {
2418
+ if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2419
+ {
2420
+ st->rangeFinal = 0;
2421
+ data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2422
+ RESTORE_STACK;
2423
+ return 1;
2424
+ }
2425
+ } else {
2426
+ st->nb_no_activity_ms_Q1 = 0;
2427
+ }
2428
+ #endif
2429
+
2430
+ /* In the unlikely case that the SILK encoder busted its target, tell
2431
+ the decoder to call the PLC */
2432
+ if (ec_tell(&enc) > (max_data_bytes-1)*8)
2433
+ {
2434
+ if (max_data_bytes < 2)
2435
+ {
2436
+ RESTORE_STACK;
2437
+ return OPUS_BUFFER_TOO_SMALL;
2438
+ }
2439
+ data[1] = 0;
2440
+ ret = 1;
2441
+ st->rangeFinal = 0;
2442
+ } else if (st->mode==MODE_SILK_ONLY&&!redundancy)
2443
+ {
2444
+ /*When in LPC only mode it's perfectly
2445
+ reasonable to strip off trailing zero bytes as
2446
+ the required range decoder behavior is to
2447
+ fill these in. This can't be done when the MDCT
2448
+ modes are used because the decoder needs to know
2449
+ the actual length for allocation purposes.*/
2450
+ while(ret>2&&data[ret]==0)ret--;
2451
+ }
2452
+ /* Count ToC and redundancy */
2453
+ ret += 1+redundancy_bytes;
2454
+ apply_padding = !st->use_vbr;
2455
+ #ifdef ENABLE_DRED
2456
+ if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2457
+ opus_extension_data extension;
2458
+ unsigned char buf[DRED_MAX_DATA_SIZE];
2459
+ int dred_chunks;
2460
+ int dred_bytes_left;
2461
+ dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2);
2462
+ if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks);
2463
+ /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2464
+ dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, max_data_bytes-ret-3);
2465
+ /* Account for the extra bytes required to signal large padding length. */
2466
+ dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2467
+ /* Check whether we actually have something to encode. */
2468
+ if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2469
+ int dred_bytes;
2470
+ #ifdef DRED_EXPERIMENTAL_VERSION
2471
+ /* Add temporary extension type and version.
2472
+ These bytes will be removed once extension is finalized. */
2473
+ buf[0] = 'D';
2474
+ buf[1] = DRED_EXPERIMENTAL_VERSION;
2475
+ #endif
2476
+ dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2477
+ st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2478
+ if (dred_bytes > 0) {
2479
+ dred_bytes += DRED_EXPERIMENTAL_BYTES;
2480
+ celt_assert(dred_bytes <= dred_bytes_left);
2481
+ extension.id = DRED_EXTENSION_ID;
2482
+ extension.frame = 0;
2483
+ extension.data = buf;
2484
+ extension.len = dred_bytes;
2485
+ ret = opus_packet_pad_impl(data, ret, max_data_bytes, !st->use_vbr, &extension, 1);
2486
+ if (ret < 0)
2487
+ {
2488
+ RESTORE_STACK;
2489
+ return OPUS_INTERNAL_ERROR;
2490
+ }
2491
+ apply_padding = 0;
2492
+ }
2493
+ }
2494
+ }
2495
+ #else
2496
+ (void)first_frame; /* Avoids a warning about first_frame being unused. */
2497
+ #endif
2498
+ if (apply_padding)
2499
+ {
2500
+ if (opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK)
2501
+ {
2502
+ RESTORE_STACK;
2503
+ return OPUS_INTERNAL_ERROR;
2504
+ }
2505
+ ret = max_data_bytes;
2506
+ }
2507
+ RESTORE_STACK;
2508
+ return ret;
2509
+ }
2510
+
2511
+
2512
+
2513
+ #if defined(FIXED_POINT) && !defined(ENABLE_RES24)
2514
+ opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2515
+ unsigned char *data, opus_int32 max_data_bytes)
2516
+ {
2517
+ int frame_size;
2518
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2519
+ return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16,
2520
+ pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2521
+ }
2522
+ #else
2523
+ opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2524
+ unsigned char *data, opus_int32 max_data_bytes)
2525
+ {
2526
+ int i, ret;
2527
+ int frame_size;
2528
+ VARDECL(opus_res, in);
2529
+ ALLOC_STACK;
2530
+
2531
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2532
+ if (frame_size <= 0)
2533
+ {
2534
+ RESTORE_STACK;
2535
+ return OPUS_BAD_ARG;
2536
+ }
2537
+ ALLOC(in, frame_size*st->channels, opus_res);
2538
+
2539
+ for (i=0;i<frame_size*st->channels;i++)
2540
+ in[i] = INT16TORES(pcm[i]);
2541
+ ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2542
+ pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 1);
2543
+ RESTORE_STACK;
2544
+ return ret;
2545
+ }
2546
+ #endif
2547
+
2548
+ #if defined(FIXED_POINT) && defined(ENABLE_RES24)
2549
+ opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
2550
+ unsigned char *data, opus_int32 max_data_bytes)
2551
+ {
2552
+ int frame_size;
2553
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2554
+ return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
2555
+ pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0);
2556
+ }
2557
+ #else
2558
+ opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
2559
+ unsigned char *data, opus_int32 max_data_bytes)
2560
+ {
2561
+ int i, ret;
2562
+ int frame_size;
2563
+ VARDECL(opus_res, in);
2564
+ ALLOC_STACK;
2565
+
2566
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2567
+ if (frame_size <= 0)
2568
+ {
2569
+ RESTORE_STACK;
2570
+ return OPUS_BAD_ARG;
2571
+ }
2572
+ ALLOC(in, frame_size*st->channels, opus_res);
2573
+
2574
+ for (i=0;i<frame_size*st->channels;i++)
2575
+ in[i] = INT24TORES(pcm[i]);
2576
+ ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
2577
+ pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 1);
2578
+ RESTORE_STACK;
2579
+ return ret;
2580
+ }
2581
+ #endif
2582
+
2583
+
2584
+ #ifndef DISABLE_FLOAT_API
2585
+
2586
+ # if !defined(FIXED_POINT)
2587
+ opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2588
+ unsigned char *data, opus_int32 out_data_bytes)
2589
+ {
2590
+ int frame_size;
2591
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2592
+ return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, MAX_ENCODING_DEPTH,
2593
+ pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2594
+ }
2595
+ # else
2596
+ opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2597
+ unsigned char *data, opus_int32 max_data_bytes)
2598
+ {
2599
+ int i, ret;
2600
+ int frame_size;
2601
+ VARDECL(opus_res, in);
2602
+ ALLOC_STACK;
2603
+
2604
+ frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
2605
+ if (frame_size <= 0)
2606
+ {
2607
+ RESTORE_STACK;
2608
+ return OPUS_BAD_ARG;
2609
+ }
2610
+ ALLOC(in, frame_size*st->channels, opus_res);
2611
+
2612
+ for (i=0;i<frame_size*st->channels;i++)
2613
+ in[i] = FLOAT2RES(pcm[i]);
2614
+ ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
2615
+ pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2616
+ RESTORE_STACK;
2617
+ return ret;
2618
+ }
2619
+ # endif
2620
+
2621
+ #endif
2622
+
2623
+
2624
+ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2625
+ {
2626
+ int ret;
2627
+ CELTEncoder *celt_enc;
2628
+ va_list ap;
2629
+
2630
+ ret = OPUS_OK;
2631
+ va_start(ap, request);
2632
+
2633
+ celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
2634
+
2635
+ switch (request)
2636
+ {
2637
+ case OPUS_SET_APPLICATION_REQUEST:
2638
+ {
2639
+ opus_int32 value = va_arg(ap, opus_int32);
2640
+ if ( (value != OPUS_APPLICATION_VOIP && value != OPUS_APPLICATION_AUDIO
2641
+ && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2642
+ || (!st->first && st->application != value))
2643
+ {
2644
+ ret = OPUS_BAD_ARG;
2645
+ break;
2646
+ }
2647
+ st->application = value;
2648
+ #ifndef DISABLE_FLOAT_API
2649
+ st->analysis.application = value;
2650
+ #endif
2651
+ }
2652
+ break;
2653
+ case OPUS_GET_APPLICATION_REQUEST:
2654
+ {
2655
+ opus_int32 *value = va_arg(ap, opus_int32*);
2656
+ if (!value)
2657
+ {
2658
+ goto bad_arg;
2659
+ }
2660
+ *value = st->application;
2661
+ }
2662
+ break;
2663
+ case OPUS_SET_BITRATE_REQUEST:
2664
+ {
2665
+ opus_int32 value = va_arg(ap, opus_int32);
2666
+ if (value != OPUS_AUTO && value != OPUS_BITRATE_MAX)
2667
+ {
2668
+ if (value <= 0)
2669
+ goto bad_arg;
2670
+ else if (value <= 500)
2671
+ value = 500;
2672
+ else if (value > (opus_int32)300000*st->channels)
2673
+ value = (opus_int32)300000*st->channels;
2674
+ }
2675
+ st->user_bitrate_bps = value;
2676
+ }
2677
+ break;
2678
+ case OPUS_GET_BITRATE_REQUEST:
2679
+ {
2680
+ opus_int32 *value = va_arg(ap, opus_int32*);
2681
+ if (!value)
2682
+ {
2683
+ goto bad_arg;
2684
+ }
2685
+ *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2686
+ }
2687
+ break;
2688
+ case OPUS_SET_FORCE_CHANNELS_REQUEST:
2689
+ {
2690
+ opus_int32 value = va_arg(ap, opus_int32);
2691
+ if((value<1 || value>st->channels) && value != OPUS_AUTO)
2692
+ {
2693
+ goto bad_arg;
2694
+ }
2695
+ st->force_channels = value;
2696
+ }
2697
+ break;
2698
+ case OPUS_GET_FORCE_CHANNELS_REQUEST:
2699
+ {
2700
+ opus_int32 *value = va_arg(ap, opus_int32*);
2701
+ if (!value)
2702
+ {
2703
+ goto bad_arg;
2704
+ }
2705
+ *value = st->force_channels;
2706
+ }
2707
+ break;
2708
+ case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2709
+ {
2710
+ opus_int32 value = va_arg(ap, opus_int32);
2711
+ if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND)
2712
+ {
2713
+ goto bad_arg;
2714
+ }
2715
+ st->max_bandwidth = value;
2716
+ if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2717
+ st->silk_mode.maxInternalSampleRate = 8000;
2718
+ } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2719
+ st->silk_mode.maxInternalSampleRate = 12000;
2720
+ } else {
2721
+ st->silk_mode.maxInternalSampleRate = 16000;
2722
+ }
2723
+ }
2724
+ break;
2725
+ case OPUS_GET_MAX_BANDWIDTH_REQUEST:
2726
+ {
2727
+ opus_int32 *value = va_arg(ap, opus_int32*);
2728
+ if (!value)
2729
+ {
2730
+ goto bad_arg;
2731
+ }
2732
+ *value = st->max_bandwidth;
2733
+ }
2734
+ break;
2735
+ case OPUS_SET_BANDWIDTH_REQUEST:
2736
+ {
2737
+ opus_int32 value = va_arg(ap, opus_int32);
2738
+ if ((value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULLBAND) && value != OPUS_AUTO)
2739
+ {
2740
+ goto bad_arg;
2741
+ }
2742
+ st->user_bandwidth = value;
2743
+ if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2744
+ st->silk_mode.maxInternalSampleRate = 8000;
2745
+ } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2746
+ st->silk_mode.maxInternalSampleRate = 12000;
2747
+ } else {
2748
+ st->silk_mode.maxInternalSampleRate = 16000;
2749
+ }
2750
+ }
2751
+ break;
2752
+ case OPUS_GET_BANDWIDTH_REQUEST:
2753
+ {
2754
+ opus_int32 *value = va_arg(ap, opus_int32*);
2755
+ if (!value)
2756
+ {
2757
+ goto bad_arg;
2758
+ }
2759
+ *value = st->bandwidth;
2760
+ }
2761
+ break;
2762
+ case OPUS_SET_DTX_REQUEST:
2763
+ {
2764
+ opus_int32 value = va_arg(ap, opus_int32);
2765
+ if(value<0 || value>1)
2766
+ {
2767
+ goto bad_arg;
2768
+ }
2769
+ st->use_dtx = value;
2770
+ }
2771
+ break;
2772
+ case OPUS_GET_DTX_REQUEST:
2773
+ {
2774
+ opus_int32 *value = va_arg(ap, opus_int32*);
2775
+ if (!value)
2776
+ {
2777
+ goto bad_arg;
2778
+ }
2779
+ *value = st->use_dtx;
2780
+ }
2781
+ break;
2782
+ case OPUS_SET_COMPLEXITY_REQUEST:
2783
+ {
2784
+ opus_int32 value = va_arg(ap, opus_int32);
2785
+ if(value<0 || value>10)
2786
+ {
2787
+ goto bad_arg;
2788
+ }
2789
+ st->silk_mode.complexity = value;
2790
+ celt_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value));
2791
+ }
2792
+ break;
2793
+ case OPUS_GET_COMPLEXITY_REQUEST:
2794
+ {
2795
+ opus_int32 *value = va_arg(ap, opus_int32*);
2796
+ if (!value)
2797
+ {
2798
+ goto bad_arg;
2799
+ }
2800
+ *value = st->silk_mode.complexity;
2801
+ }
2802
+ break;
2803
+ case OPUS_SET_INBAND_FEC_REQUEST:
2804
+ {
2805
+ opus_int32 value = va_arg(ap, opus_int32);
2806
+ if(value<0 || value>2)
2807
+ {
2808
+ goto bad_arg;
2809
+ }
2810
+ st->fec_config = value;
2811
+ st->silk_mode.useInBandFEC = (value != 0);
2812
+ }
2813
+ break;
2814
+ case OPUS_GET_INBAND_FEC_REQUEST:
2815
+ {
2816
+ opus_int32 *value = va_arg(ap, opus_int32*);
2817
+ if (!value)
2818
+ {
2819
+ goto bad_arg;
2820
+ }
2821
+ *value = st->fec_config;
2822
+ }
2823
+ break;
2824
+ case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2825
+ {
2826
+ opus_int32 value = va_arg(ap, opus_int32);
2827
+ if (value < 0 || value > 100)
2828
+ {
2829
+ goto bad_arg;
2830
+ }
2831
+ st->silk_mode.packetLossPercentage = value;
2832
+ celt_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value));
2833
+ }
2834
+ break;
2835
+ case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
2836
+ {
2837
+ opus_int32 *value = va_arg(ap, opus_int32*);
2838
+ if (!value)
2839
+ {
2840
+ goto bad_arg;
2841
+ }
2842
+ *value = st->silk_mode.packetLossPercentage;
2843
+ }
2844
+ break;
2845
+ case OPUS_SET_VBR_REQUEST:
2846
+ {
2847
+ opus_int32 value = va_arg(ap, opus_int32);
2848
+ if(value<0 || value>1)
2849
+ {
2850
+ goto bad_arg;
2851
+ }
2852
+ st->use_vbr = value;
2853
+ st->silk_mode.useCBR = 1-value;
2854
+ }
2855
+ break;
2856
+ case OPUS_GET_VBR_REQUEST:
2857
+ {
2858
+ opus_int32 *value = va_arg(ap, opus_int32*);
2859
+ if (!value)
2860
+ {
2861
+ goto bad_arg;
2862
+ }
2863
+ *value = st->use_vbr;
2864
+ }
2865
+ break;
2866
+ case OPUS_SET_VOICE_RATIO_REQUEST:
2867
+ {
2868
+ opus_int32 value = va_arg(ap, opus_int32);
2869
+ if (value<-1 || value>100)
2870
+ {
2871
+ goto bad_arg;
2872
+ }
2873
+ st->voice_ratio = value;
2874
+ }
2875
+ break;
2876
+ case OPUS_GET_VOICE_RATIO_REQUEST:
2877
+ {
2878
+ opus_int32 *value = va_arg(ap, opus_int32*);
2879
+ if (!value)
2880
+ {
2881
+ goto bad_arg;
2882
+ }
2883
+ *value = st->voice_ratio;
2884
+ }
2885
+ break;
2886
+ case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2887
+ {
2888
+ opus_int32 value = va_arg(ap, opus_int32);
2889
+ if(value<0 || value>1)
2890
+ {
2891
+ goto bad_arg;
2892
+ }
2893
+ st->vbr_constraint = value;
2894
+ }
2895
+ break;
2896
+ case OPUS_GET_VBR_CONSTRAINT_REQUEST:
2897
+ {
2898
+ opus_int32 *value = va_arg(ap, opus_int32*);
2899
+ if (!value)
2900
+ {
2901
+ goto bad_arg;
2902
+ }
2903
+ *value = st->vbr_constraint;
2904
+ }
2905
+ break;
2906
+ case OPUS_SET_SIGNAL_REQUEST:
2907
+ {
2908
+ opus_int32 value = va_arg(ap, opus_int32);
2909
+ if(value!=OPUS_AUTO && value!=OPUS_SIGNAL_VOICE && value!=OPUS_SIGNAL_MUSIC)
2910
+ {
2911
+ goto bad_arg;
2912
+ }
2913
+ st->signal_type = value;
2914
+ }
2915
+ break;
2916
+ case OPUS_GET_SIGNAL_REQUEST:
2917
+ {
2918
+ opus_int32 *value = va_arg(ap, opus_int32*);
2919
+ if (!value)
2920
+ {
2921
+ goto bad_arg;
2922
+ }
2923
+ *value = st->signal_type;
2924
+ }
2925
+ break;
2926
+ case OPUS_GET_LOOKAHEAD_REQUEST:
2927
+ {
2928
+ opus_int32 *value = va_arg(ap, opus_int32*);
2929
+ if (!value)
2930
+ {
2931
+ goto bad_arg;
2932
+ }
2933
+ *value = st->Fs/400;
2934
+ if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2935
+ *value += st->delay_compensation;
2936
+ }
2937
+ break;
2938
+ case OPUS_GET_SAMPLE_RATE_REQUEST:
2939
+ {
2940
+ opus_int32 *value = va_arg(ap, opus_int32*);
2941
+ if (!value)
2942
+ {
2943
+ goto bad_arg;
2944
+ }
2945
+ *value = st->Fs;
2946
+ }
2947
+ break;
2948
+ case OPUS_GET_FINAL_RANGE_REQUEST:
2949
+ {
2950
+ opus_uint32 *value = va_arg(ap, opus_uint32*);
2951
+ if (!value)
2952
+ {
2953
+ goto bad_arg;
2954
+ }
2955
+ *value = st->rangeFinal;
2956
+ }
2957
+ break;
2958
+ case OPUS_SET_LSB_DEPTH_REQUEST:
2959
+ {
2960
+ opus_int32 value = va_arg(ap, opus_int32);
2961
+ if (value<8 || value>24)
2962
+ {
2963
+ goto bad_arg;
2964
+ }
2965
+ st->lsb_depth=value;
2966
+ }
2967
+ break;
2968
+ case OPUS_GET_LSB_DEPTH_REQUEST:
2969
+ {
2970
+ opus_int32 *value = va_arg(ap, opus_int32*);
2971
+ if (!value)
2972
+ {
2973
+ goto bad_arg;
2974
+ }
2975
+ *value = st->lsb_depth;
2976
+ }
2977
+ break;
2978
+ case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2979
+ {
2980
+ opus_int32 value = va_arg(ap, opus_int32);
2981
+ if (value != OPUS_FRAMESIZE_ARG && value != OPUS_FRAMESIZE_2_5_MS &&
2982
+ value != OPUS_FRAMESIZE_5_MS && value != OPUS_FRAMESIZE_10_MS &&
2983
+ value != OPUS_FRAMESIZE_20_MS && value != OPUS_FRAMESIZE_40_MS &&
2984
+ value != OPUS_FRAMESIZE_60_MS && value != OPUS_FRAMESIZE_80_MS &&
2985
+ value != OPUS_FRAMESIZE_100_MS && value != OPUS_FRAMESIZE_120_MS)
2986
+ {
2987
+ goto bad_arg;
2988
+ }
2989
+ st->variable_duration = value;
2990
+ }
2991
+ break;
2992
+ case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
2993
+ {
2994
+ opus_int32 *value = va_arg(ap, opus_int32*);
2995
+ if (!value)
2996
+ {
2997
+ goto bad_arg;
2998
+ }
2999
+ *value = st->variable_duration;
3000
+ }
3001
+ break;
3002
+ case OPUS_SET_PREDICTION_DISABLED_REQUEST:
3003
+ {
3004
+ opus_int32 value = va_arg(ap, opus_int32);
3005
+ if (value > 1 || value < 0)
3006
+ goto bad_arg;
3007
+ st->silk_mode.reducedDependency = value;
3008
+ }
3009
+ break;
3010
+ case OPUS_GET_PREDICTION_DISABLED_REQUEST:
3011
+ {
3012
+ opus_int32 *value = va_arg(ap, opus_int32*);
3013
+ if (!value)
3014
+ goto bad_arg;
3015
+ *value = st->silk_mode.reducedDependency;
3016
+ }
3017
+ break;
3018
+ case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
3019
+ {
3020
+ opus_int32 value = va_arg(ap, opus_int32);
3021
+ if(value<0 || value>1)
3022
+ {
3023
+ goto bad_arg;
3024
+ }
3025
+ celt_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value));
3026
+ }
3027
+ break;
3028
+ case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3029
+ {
3030
+ opus_int32 *value = va_arg(ap, opus_int32*);
3031
+ if (!value)
3032
+ {
3033
+ goto bad_arg;
3034
+ }
3035
+ celt_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value));
3036
+ }
3037
+ break;
3038
+ #ifdef ENABLE_DRED
3039
+ case OPUS_SET_DRED_DURATION_REQUEST:
3040
+ {
3041
+ opus_int32 value = va_arg(ap, opus_int32);
3042
+ if(value<0 || value>DRED_MAX_FRAMES)
3043
+ {
3044
+ goto bad_arg;
3045
+ }
3046
+ st->dred_duration = value;
3047
+ st->silk_mode.useDRED = !!value;
3048
+ }
3049
+ break;
3050
+ case OPUS_GET_DRED_DURATION_REQUEST:
3051
+ {
3052
+ opus_int32 *value = va_arg(ap, opus_int32*);
3053
+ if (!value)
3054
+ {
3055
+ goto bad_arg;
3056
+ }
3057
+ *value = st->dred_duration;
3058
+ }
3059
+ break;
3060
+ #endif
3061
+ case OPUS_RESET_STATE:
3062
+ {
3063
+ void *silk_enc;
3064
+ silk_EncControlStruct dummy;
3065
+ char *start;
3066
+ silk_enc = (char*)st+st->silk_enc_offset;
3067
+ #ifndef DISABLE_FLOAT_API
3068
+ tonality_analysis_reset(&st->analysis);
3069
+ #endif
3070
+
3071
+ start = (char*)&st->OPUS_ENCODER_RESET_START;
3072
+ OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
3073
+
3074
+ celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
3075
+ silk_InitEncoder( silk_enc, st->arch, &dummy );
3076
+ #ifdef ENABLE_DRED
3077
+ /* Initialize DRED Encoder */
3078
+ dred_encoder_reset( &st->dred_encoder );
3079
+ #endif
3080
+ st->stream_channels = st->channels;
3081
+ st->hybrid_stereo_width_Q14 = 1 << 14;
3082
+ st->prev_HB_gain = Q15ONE;
3083
+ st->first = 1;
3084
+ st->mode = MODE_HYBRID;
3085
+ st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
3086
+ st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
3087
+ }
3088
+ break;
3089
+ case OPUS_SET_FORCE_MODE_REQUEST:
3090
+ {
3091
+ opus_int32 value = va_arg(ap, opus_int32);
3092
+ if ((value < MODE_SILK_ONLY || value > MODE_CELT_ONLY) && value != OPUS_AUTO)
3093
+ {
3094
+ goto bad_arg;
3095
+ }
3096
+ st->user_forced_mode = value;
3097
+ }
3098
+ break;
3099
+ case OPUS_SET_LFE_REQUEST:
3100
+ {
3101
+ opus_int32 value = va_arg(ap, opus_int32);
3102
+ st->lfe = value;
3103
+ ret = celt_encoder_ctl(celt_enc, OPUS_SET_LFE(value));
3104
+ }
3105
+ break;
3106
+ case OPUS_SET_ENERGY_MASK_REQUEST:
3107
+ {
3108
+ celt_glog *value = va_arg(ap, celt_glog*);
3109
+ st->energy_masking = value;
3110
+ ret = celt_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value));
3111
+ }
3112
+ break;
3113
+ case OPUS_GET_IN_DTX_REQUEST:
3114
+ {
3115
+ opus_int32 *value = va_arg(ap, opus_int32*);
3116
+ if (!value)
3117
+ {
3118
+ goto bad_arg;
3119
+ }
3120
+ if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY || st->prev_mode == MODE_HYBRID)) {
3121
+ /* DTX determined by Silk. */
3122
+ silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset);
3123
+ *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3124
+ /* Stereo: check second channel unless only the middle channel was encoded. */
3125
+ if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) {
3126
+ *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX;
3127
+ }
3128
+ }
3129
+ #ifndef DISABLE_FLOAT_API
3130
+ else if (st->use_dtx) {
3131
+ /* DTX determined by Opus. */
3132
+ *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX*20*2;
3133
+ }
3134
+ #endif
3135
+ else {
3136
+ *value = 0;
3137
+ }
3138
+ }
3139
+ break;
3140
+ #ifdef USE_WEIGHTS_FILE
3141
+ case OPUS_SET_DNN_BLOB_REQUEST:
3142
+ {
3143
+ const unsigned char *data = va_arg(ap, const unsigned char *);
3144
+ opus_int32 len = va_arg(ap, opus_int32);
3145
+ if(len<0 || data == NULL)
3146
+ {
3147
+ goto bad_arg;
3148
+ }
3149
+ #ifdef ENABLE_DRED
3150
+ ret = dred_encoder_load_model(&st->dred_encoder, data, len);
3151
+ #endif
3152
+ }
3153
+ break;
3154
+ #endif
3155
+ case CELT_GET_MODE_REQUEST:
3156
+ {
3157
+ const CELTMode ** value = va_arg(ap, const CELTMode**);
3158
+ if (!value)
3159
+ {
3160
+ goto bad_arg;
3161
+ }
3162
+ ret = celt_encoder_ctl(celt_enc, CELT_GET_MODE(value));
3163
+ }
3164
+ break;
3165
+ default:
3166
+ /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
3167
+ ret = OPUS_UNIMPLEMENTED;
3168
+ break;
3169
+ }
3170
+ va_end(ap);
3171
+ return ret;
3172
+ bad_arg:
3173
+ va_end(ap);
3174
+ return OPUS_BAD_ARG;
3175
+ }
3176
+
3177
+ void opus_encoder_destroy(OpusEncoder *st)
3178
+ {
3179
+ opus_free(st);
3180
+ }