You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1229 lines
41KB

  1. /*
  2. * Copyright (c) 2017 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/audio_fifo.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "libavcodec/avfft.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "formats.h"
  27. typedef struct AudioSurroundContext {
  28. const AVClass *class;
  29. char *out_channel_layout_str;
  30. char *in_channel_layout_str;
  31. float level_in;
  32. float level_out;
  33. int output_lfe;
  34. int lowcutf;
  35. int highcutf;
  36. float lowcut;
  37. float highcut;
  38. uint64_t out_channel_layout;
  39. uint64_t in_channel_layout;
  40. int nb_in_channels;
  41. int nb_out_channels;
  42. AVFrame *input;
  43. AVFrame *output;
  44. AVFrame *overlap_buffer;
  45. int buf_size;
  46. int hop_size;
  47. AVAudioFifo *fifo;
  48. RDFTContext **rdft, **irdft;
  49. float *window_func_lut;
  50. int64_t pts;
  51. void (*filter)(AVFilterContext *ctx);
  52. void (*upmix_stereo)(AVFilterContext *ctx,
  53. float l_phase,
  54. float r_phase,
  55. float c_phase,
  56. float mag_total,
  57. float x, float y,
  58. int n);
  59. void (*upmix_2_1)(AVFilterContext *ctx,
  60. float l_phase,
  61. float r_phase,
  62. float c_phase,
  63. float mag_total,
  64. float lfe_im,
  65. float lfe_re,
  66. float x, float y,
  67. int n);
  68. void (*upmix_3_0)(AVFilterContext *ctx,
  69. float l_phase,
  70. float r_phase,
  71. float c_mag,
  72. float c_phase,
  73. float mag_total,
  74. float x, float y,
  75. int n);
  76. void (*upmix_5_1)(AVFilterContext *ctx,
  77. float c_re, float c_im,
  78. float lfe_re, float lfe_im,
  79. float mag_totall, float mag_totalr,
  80. float fl_phase, float fr_phase,
  81. float bl_phase, float br_phase,
  82. float sl_phase, float sr_phase,
  83. float xl, float yl,
  84. float xr, float yr,
  85. int n);
  86. } AudioSurroundContext;
  87. static int query_formats(AVFilterContext *ctx)
  88. {
  89. AudioSurroundContext *s = ctx->priv;
  90. AVFilterFormats *formats = NULL;
  91. AVFilterChannelLayouts *layouts = NULL;
  92. int ret;
  93. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
  94. if (ret)
  95. return ret;
  96. ret = ff_set_common_formats(ctx, formats);
  97. if (ret)
  98. return ret;
  99. layouts = NULL;
  100. ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
  101. if (ret)
  102. return ret;
  103. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  104. if (ret)
  105. return ret;
  106. layouts = NULL;
  107. ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
  108. if (ret)
  109. return ret;
  110. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  111. if (ret)
  112. return ret;
  113. formats = ff_all_samplerates();
  114. if (!formats)
  115. return AVERROR(ENOMEM);
  116. return ff_set_common_samplerates(ctx, formats);
  117. }
  118. static int config_input(AVFilterLink *inlink)
  119. {
  120. AVFilterContext *ctx = inlink->dst;
  121. AudioSurroundContext *s = ctx->priv;
  122. int ch;
  123. s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
  124. if (!s->rdft)
  125. return AVERROR(ENOMEM);
  126. for (ch = 0; ch < inlink->channels; ch++) {
  127. s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
  128. if (!s->rdft[ch])
  129. return AVERROR(ENOMEM);
  130. }
  131. s->nb_in_channels = inlink->channels;
  132. s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
  133. if (!s->input)
  134. return AVERROR(ENOMEM);
  135. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
  136. if (!s->fifo)
  137. return AVERROR(ENOMEM);
  138. s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
  139. s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
  140. return 0;
  141. }
  142. static int config_output(AVFilterLink *outlink)
  143. {
  144. AVFilterContext *ctx = outlink->src;
  145. AudioSurroundContext *s = ctx->priv;
  146. int ch;
  147. s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
  148. if (!s->irdft)
  149. return AVERROR(ENOMEM);
  150. for (ch = 0; ch < outlink->channels; ch++) {
  151. s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
  152. if (!s->irdft[ch])
  153. return AVERROR(ENOMEM);
  154. }
  155. s->nb_out_channels = outlink->channels;
  156. s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
  157. s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
  158. if (!s->overlap_buffer || !s->output)
  159. return AVERROR(ENOMEM);
  160. return 0;
  161. }
  162. static void stereo_position(float a, float p, float *x, float *y)
  163. {
  164. *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
  165. *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
  166. }
  167. static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
  168. float *lfe_mag, float *mag_total)
  169. {
  170. if (output_lfe && n < highcut) {
  171. *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
  172. *lfe_mag *= *mag_total;
  173. *mag_total -= *lfe_mag;
  174. } else {
  175. *lfe_mag = 0.f;
  176. }
  177. }
  178. static void upmix_1_0(AVFilterContext *ctx,
  179. float l_phase,
  180. float r_phase,
  181. float c_phase,
  182. float mag_total,
  183. float x, float y,
  184. int n)
  185. {
  186. AudioSurroundContext *s = ctx->priv;
  187. float mag, *dst;
  188. dst = (float *)s->output->extended_data[0];
  189. mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  190. dst[2 * n ] = mag * cosf(c_phase);
  191. dst[2 * n + 1] = mag * sinf(c_phase);
  192. }
  193. static void upmix_stereo(AVFilterContext *ctx,
  194. float l_phase,
  195. float r_phase,
  196. float c_phase,
  197. float mag_total,
  198. float x, float y,
  199. int n)
  200. {
  201. AudioSurroundContext *s = ctx->priv;
  202. float l_mag, r_mag, *dstl, *dstr;
  203. dstl = (float *)s->output->extended_data[0];
  204. dstr = (float *)s->output->extended_data[1];
  205. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  206. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  207. dstl[2 * n ] = l_mag * cosf(l_phase);
  208. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  209. dstr[2 * n ] = r_mag * cosf(r_phase);
  210. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  211. }
  212. static void upmix_2_1(AVFilterContext *ctx,
  213. float l_phase,
  214. float r_phase,
  215. float c_phase,
  216. float mag_total,
  217. float x, float y,
  218. int n)
  219. {
  220. AudioSurroundContext *s = ctx->priv;
  221. float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
  222. dstl = (float *)s->output->extended_data[0];
  223. dstr = (float *)s->output->extended_data[1];
  224. dstlfe = (float *)s->output->extended_data[2];
  225. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  226. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  227. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  228. dstl[2 * n ] = l_mag * cosf(l_phase);
  229. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  230. dstr[2 * n ] = r_mag * cosf(r_phase);
  231. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  232. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  233. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  234. }
  235. static void upmix_3_0(AVFilterContext *ctx,
  236. float l_phase,
  237. float r_phase,
  238. float c_phase,
  239. float mag_total,
  240. float x, float y,
  241. int n)
  242. {
  243. AudioSurroundContext *s = ctx->priv;
  244. float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
  245. dstl = (float *)s->output->extended_data[0];
  246. dstr = (float *)s->output->extended_data[1];
  247. dstc = (float *)s->output->extended_data[2];
  248. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  249. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  250. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  251. dstl[2 * n ] = l_mag * cosf(l_phase);
  252. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  253. dstr[2 * n ] = r_mag * cosf(r_phase);
  254. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  255. dstc[2 * n ] = c_mag * cosf(c_phase);
  256. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  257. }
  258. static void upmix_3_1(AVFilterContext *ctx,
  259. float l_phase,
  260. float r_phase,
  261. float c_phase,
  262. float mag_total,
  263. float x, float y,
  264. int n)
  265. {
  266. AudioSurroundContext *s = ctx->priv;
  267. float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
  268. dstl = (float *)s->output->extended_data[0];
  269. dstr = (float *)s->output->extended_data[1];
  270. dstc = (float *)s->output->extended_data[2];
  271. dstlfe = (float *)s->output->extended_data[3];
  272. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  273. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  274. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  275. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  276. dstl[2 * n ] = l_mag * cosf(l_phase);
  277. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  278. dstr[2 * n ] = r_mag * cosf(r_phase);
  279. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  280. dstc[2 * n ] = c_mag * cosf(c_phase);
  281. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  282. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  283. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  284. }
  285. static void upmix_3_1_surround(AVFilterContext *ctx,
  286. float l_phase,
  287. float r_phase,
  288. float c_phase,
  289. float c_mag,
  290. float mag_total,
  291. float x, float y,
  292. int n)
  293. {
  294. AudioSurroundContext *s = ctx->priv;
  295. float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
  296. dstl = (float *)s->output->extended_data[0];
  297. dstr = (float *)s->output->extended_data[1];
  298. dstc = (float *)s->output->extended_data[2];
  299. dstlfe = (float *)s->output->extended_data[3];
  300. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
  301. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  302. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  303. dstl[2 * n ] = l_mag * cosf(l_phase);
  304. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  305. dstr[2 * n ] = r_mag * cosf(r_phase);
  306. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  307. dstc[2 * n ] = c_mag * cosf(c_phase);
  308. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  309. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  310. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  311. }
  312. static void upmix_4_0(AVFilterContext *ctx,
  313. float l_phase,
  314. float r_phase,
  315. float c_phase,
  316. float mag_total,
  317. float x, float y,
  318. int n)
  319. {
  320. float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
  321. AudioSurroundContext *s = ctx->priv;
  322. dstl = (float *)s->output->extended_data[0];
  323. dstr = (float *)s->output->extended_data[1];
  324. dstc = (float *)s->output->extended_data[2];
  325. dstb = (float *)s->output->extended_data[3];
  326. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  327. b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
  328. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  329. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  330. dstl[2 * n ] = l_mag * cosf(l_phase);
  331. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  332. dstr[2 * n ] = r_mag * cosf(r_phase);
  333. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  334. dstc[2 * n ] = c_mag * cosf(c_phase);
  335. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  336. dstb[2 * n ] = b_mag * cosf(c_phase);
  337. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  338. }
  339. static void upmix_4_1(AVFilterContext *ctx,
  340. float l_phase,
  341. float r_phase,
  342. float c_phase,
  343. float mag_total,
  344. float x, float y,
  345. int n)
  346. {
  347. float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
  348. AudioSurroundContext *s = ctx->priv;
  349. dstl = (float *)s->output->extended_data[0];
  350. dstr = (float *)s->output->extended_data[1];
  351. dstc = (float *)s->output->extended_data[2];
  352. dstlfe = (float *)s->output->extended_data[3];
  353. dstb = (float *)s->output->extended_data[4];
  354. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  355. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  356. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  357. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  358. b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
  359. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  360. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  361. dstl[2 * n ] = l_mag * cosf(l_phase);
  362. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  363. dstr[2 * n ] = r_mag * cosf(r_phase);
  364. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  365. dstc[2 * n ] = c_mag * cosf(c_phase);
  366. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  367. dstb[2 * n ] = b_mag * cosf(c_phase);
  368. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  369. }
  370. static void upmix_5_0_back(AVFilterContext *ctx,
  371. float l_phase,
  372. float r_phase,
  373. float c_phase,
  374. float mag_total,
  375. float x, float y,
  376. int n)
  377. {
  378. float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
  379. AudioSurroundContext *s = ctx->priv;
  380. dstl = (float *)s->output->extended_data[0];
  381. dstr = (float *)s->output->extended_data[1];
  382. dstc = (float *)s->output->extended_data[2];
  383. dstls = (float *)s->output->extended_data[3];
  384. dstrs = (float *)s->output->extended_data[4];
  385. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  386. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  387. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  388. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  389. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  390. dstl[2 * n ] = l_mag * cosf(l_phase);
  391. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  392. dstr[2 * n ] = r_mag * cosf(r_phase);
  393. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  394. dstc[2 * n ] = c_mag * cosf(c_phase);
  395. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  396. dstls[2 * n ] = ls_mag * cosf(l_phase);
  397. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  398. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  399. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  400. }
  401. static void upmix_5_1_back(AVFilterContext *ctx,
  402. float l_phase,
  403. float r_phase,
  404. float c_phase,
  405. float mag_total,
  406. float x, float y,
  407. int n)
  408. {
  409. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
  410. AudioSurroundContext *s = ctx->priv;
  411. dstl = (float *)s->output->extended_data[0];
  412. dstr = (float *)s->output->extended_data[1];
  413. dstc = (float *)s->output->extended_data[2];
  414. dstlfe = (float *)s->output->extended_data[3];
  415. dstls = (float *)s->output->extended_data[4];
  416. dstrs = (float *)s->output->extended_data[5];
  417. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  418. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  419. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  420. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  421. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  422. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  423. dstl[2 * n ] = l_mag * cosf(l_phase);
  424. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  425. dstr[2 * n ] = r_mag * cosf(r_phase);
  426. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  427. dstc[2 * n ] = c_mag * cosf(c_phase);
  428. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  429. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  430. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  431. dstls[2 * n ] = ls_mag * cosf(l_phase);
  432. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  433. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  434. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  435. }
  436. static void upmix_5_1_back_surround(AVFilterContext *ctx,
  437. float l_phase,
  438. float r_phase,
  439. float c_phase,
  440. float c_mag,
  441. float mag_total,
  442. float x, float y,
  443. int n)
  444. {
  445. AudioSurroundContext *s = ctx->priv;
  446. float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
  447. float ls_mag, rs_mag, *dstls, *dstrs;
  448. dstl = (float *)s->output->extended_data[0];
  449. dstr = (float *)s->output->extended_data[1];
  450. dstc = (float *)s->output->extended_data[2];
  451. dstlfe = (float *)s->output->extended_data[3];
  452. dstls = (float *)s->output->extended_data[4];
  453. dstrs = (float *)s->output->extended_data[5];
  454. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag);
  455. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  456. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  457. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  458. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  459. dstl[2 * n ] = l_mag * cosf(l_phase);
  460. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  461. dstr[2 * n ] = r_mag * cosf(r_phase);
  462. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  463. dstc[2 * n ] = c_mag * cosf(c_phase);
  464. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  465. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  466. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  467. dstls[2 * n ] = ls_mag * cosf(l_phase);
  468. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  469. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  470. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  471. }
  472. static void upmix_5_1_back_2_1(AVFilterContext *ctx,
  473. float l_phase,
  474. float r_phase,
  475. float c_phase,
  476. float mag_total,
  477. float lfe_re,
  478. float lfe_im,
  479. float x, float y,
  480. int n)
  481. {
  482. AudioSurroundContext *s = ctx->priv;
  483. float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
  484. float ls_mag, rs_mag, *dstls, *dstrs;
  485. dstl = (float *)s->output->extended_data[0];
  486. dstr = (float *)s->output->extended_data[1];
  487. dstc = (float *)s->output->extended_data[2];
  488. dstlfe = (float *)s->output->extended_data[3];
  489. dstls = (float *)s->output->extended_data[4];
  490. dstrs = (float *)s->output->extended_data[5];
  491. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  492. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  493. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  494. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  495. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  496. dstl[2 * n ] = l_mag * cosf(l_phase);
  497. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  498. dstr[2 * n ] = r_mag * cosf(r_phase);
  499. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  500. dstc[2 * n ] = c_mag * cosf(c_phase);
  501. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  502. dstlfe[2 * n ] = lfe_re;
  503. dstlfe[2 * n + 1] = lfe_im;
  504. dstls[2 * n ] = ls_mag * cosf(l_phase);
  505. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  506. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  507. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  508. }
  509. static void upmix_7_0(AVFilterContext *ctx,
  510. float l_phase,
  511. float r_phase,
  512. float c_phase,
  513. float mag_total,
  514. float x, float y,
  515. int n)
  516. {
  517. float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
  518. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
  519. AudioSurroundContext *s = ctx->priv;
  520. dstl = (float *)s->output->extended_data[0];
  521. dstr = (float *)s->output->extended_data[1];
  522. dstc = (float *)s->output->extended_data[2];
  523. dstlb = (float *)s->output->extended_data[3];
  524. dstrb = (float *)s->output->extended_data[4];
  525. dstls = (float *)s->output->extended_data[5];
  526. dstrs = (float *)s->output->extended_data[6];
  527. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  528. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  529. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  530. lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  531. rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  532. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  533. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  534. dstl[2 * n ] = l_mag * cosf(l_phase);
  535. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  536. dstr[2 * n ] = r_mag * cosf(r_phase);
  537. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  538. dstc[2 * n ] = c_mag * cosf(c_phase);
  539. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  540. dstlb[2 * n ] = lb_mag * cosf(l_phase);
  541. dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
  542. dstrb[2 * n ] = rb_mag * cosf(r_phase);
  543. dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
  544. dstls[2 * n ] = ls_mag * cosf(l_phase);
  545. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  546. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  547. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  548. }
  549. static void upmix_7_1(AVFilterContext *ctx,
  550. float l_phase,
  551. float r_phase,
  552. float c_phase,
  553. float mag_total,
  554. float x, float y,
  555. int n)
  556. {
  557. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
  558. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  559. AudioSurroundContext *s = ctx->priv;
  560. dstl = (float *)s->output->extended_data[0];
  561. dstr = (float *)s->output->extended_data[1];
  562. dstc = (float *)s->output->extended_data[2];
  563. dstlfe = (float *)s->output->extended_data[3];
  564. dstlb = (float *)s->output->extended_data[4];
  565. dstrb = (float *)s->output->extended_data[5];
  566. dstls = (float *)s->output->extended_data[6];
  567. dstrs = (float *)s->output->extended_data[7];
  568. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  569. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  570. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  571. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  572. lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  573. rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  574. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  575. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  576. dstl[2 * n ] = l_mag * cosf(l_phase);
  577. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  578. dstr[2 * n ] = r_mag * cosf(r_phase);
  579. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  580. dstc[2 * n ] = c_mag * cosf(c_phase);
  581. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  582. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  583. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  584. dstlb[2 * n ] = lb_mag * cosf(l_phase);
  585. dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
  586. dstrb[2 * n ] = rb_mag * cosf(r_phase);
  587. dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
  588. dstls[2 * n ] = ls_mag * cosf(l_phase);
  589. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  590. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  591. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  592. }
  593. static void upmix_7_1_5_1(AVFilterContext *ctx,
  594. float c_re, float c_im,
  595. float lfe_re, float lfe_im,
  596. float mag_totall, float mag_totalr,
  597. float fl_phase, float fr_phase,
  598. float bl_phase, float br_phase,
  599. float sl_phase, float sr_phase,
  600. float xl, float yl,
  601. float xr, float yr,
  602. int n)
  603. {
  604. float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
  605. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  606. AudioSurroundContext *s = ctx->priv;
  607. dstl = (float *)s->output->extended_data[0];
  608. dstr = (float *)s->output->extended_data[1];
  609. dstc = (float *)s->output->extended_data[2];
  610. dstlfe = (float *)s->output->extended_data[3];
  611. dstlb = (float *)s->output->extended_data[4];
  612. dstrb = (float *)s->output->extended_data[5];
  613. dstls = (float *)s->output->extended_data[6];
  614. dstrs = (float *)s->output->extended_data[7];
  615. fl_mag = sqrtf(.5f * (xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
  616. fr_mag = sqrtf(.5f * (xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
  617. lb_mag = sqrtf(.5f * (-xl + 1.f)) * ((yl + 1.f) * .5f) * mag_totall;
  618. rb_mag = sqrtf(.5f * (-xr + 1.f)) * ((yr + 1.f) * .5f) * mag_totalr;
  619. ls_mag = sqrtf(1.f - fabsf(xl)) * ((yl + 1.f) * .5f) * mag_totall;
  620. rs_mag = sqrtf(1.f - fabsf(xr)) * ((yr + 1.f) * .5f) * mag_totalr;
  621. dstl[2 * n ] = fl_mag * cosf(fl_phase);
  622. dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
  623. dstr[2 * n ] = fr_mag * cosf(fr_phase);
  624. dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
  625. dstc[2 * n ] = c_re;
  626. dstc[2 * n + 1] = c_im;
  627. dstlfe[2 * n ] = lfe_re;
  628. dstlfe[2 * n + 1] = lfe_im;
  629. dstlb[2 * n ] = lb_mag * cosf(bl_phase);
  630. dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
  631. dstrb[2 * n ] = rb_mag * cosf(br_phase);
  632. dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
  633. dstls[2 * n ] = ls_mag * cosf(sl_phase);
  634. dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
  635. dstrs[2 * n ] = rs_mag * cosf(sr_phase);
  636. dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
  637. }
  638. static void filter_stereo(AVFilterContext *ctx)
  639. {
  640. AudioSurroundContext *s = ctx->priv;
  641. float *srcl, *srcr;
  642. int n;
  643. srcl = (float *)s->input->extended_data[0];
  644. srcr = (float *)s->input->extended_data[1];
  645. for (n = 0; n < s->buf_size; n++) {
  646. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  647. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  648. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  649. float l_mag = hypotf(l_re, l_im);
  650. float r_mag = hypotf(r_re, r_im);
  651. float l_phase = atan2f(l_im, l_re);
  652. float r_phase = atan2f(r_im, r_re);
  653. float phase_dif = fabsf(l_phase - r_phase);
  654. float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
  655. float mag_total = hypotf(l_mag, r_mag);
  656. float x, y;
  657. if (phase_dif > M_PI)
  658. phase_dif = 2 * M_PI - phase_dif;
  659. stereo_position(mag_dif, phase_dif, &x, &y);
  660. s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
  661. }
  662. }
  663. static void filter_surround(AVFilterContext *ctx)
  664. {
  665. AudioSurroundContext *s = ctx->priv;
  666. float *srcl, *srcr, *srcc;
  667. int n;
  668. srcl = (float *)s->input->extended_data[0];
  669. srcr = (float *)s->input->extended_data[1];
  670. srcc = (float *)s->input->extended_data[2];
  671. for (n = 0; n < s->buf_size; n++) {
  672. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  673. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  674. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  675. float c_mag = hypotf(c_re, c_im);
  676. float c_phase = atan2f(c_im, c_re);
  677. float l_mag = hypotf(l_re, l_im);
  678. float r_mag = hypotf(r_re, r_im);
  679. float l_phase = atan2f(l_im, l_re);
  680. float r_phase = atan2f(r_im, r_re);
  681. float phase_dif = fabsf(l_phase - r_phase);
  682. float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
  683. float mag_total = hypotf(l_mag, r_mag);
  684. float x, y;
  685. if (phase_dif > M_PI)
  686. phase_dif = 2 * M_PI - phase_dif;
  687. stereo_position(mag_dif, phase_dif, &x, &y);
  688. s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
  689. }
  690. }
  691. static void filter_2_1(AVFilterContext *ctx)
  692. {
  693. AudioSurroundContext *s = ctx->priv;
  694. float *srcl, *srcr, *srclfe;
  695. int n;
  696. srcl = (float *)s->input->extended_data[0];
  697. srcr = (float *)s->input->extended_data[1];
  698. srclfe = (float *)s->input->extended_data[2];
  699. for (n = 0; n < s->buf_size; n++) {
  700. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  701. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  702. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  703. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  704. float l_mag = hypotf(l_re, l_im);
  705. float r_mag = hypotf(r_re, r_im);
  706. float l_phase = atan2f(l_im, l_re);
  707. float r_phase = atan2f(r_im, r_re);
  708. float phase_dif = fabsf(l_phase - r_phase);
  709. float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
  710. float mag_total = hypotf(l_mag, r_mag);
  711. float x, y;
  712. if (phase_dif > M_PI)
  713. phase_dif = 2 * M_PI - phase_dif;
  714. stereo_position(mag_dif, phase_dif, &x, &y);
  715. s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
  716. }
  717. }
  718. static void filter_5_1_back(AVFilterContext *ctx)
  719. {
  720. AudioSurroundContext *s = ctx->priv;
  721. float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
  722. int n;
  723. srcl = (float *)s->input->extended_data[0];
  724. srcr = (float *)s->input->extended_data[1];
  725. srcc = (float *)s->input->extended_data[2];
  726. srclfe = (float *)s->input->extended_data[3];
  727. srcbl = (float *)s->input->extended_data[4];
  728. srcbr = (float *)s->input->extended_data[5];
  729. for (n = 0; n < s->buf_size; n++) {
  730. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  731. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  732. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  733. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  734. float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
  735. float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
  736. float fl_mag = hypotf(fl_re, fl_im);
  737. float fr_mag = hypotf(fr_re, fr_im);
  738. float fl_phase = atan2f(fl_im, fl_re);
  739. float fr_phase = atan2f(fr_im, fr_re);
  740. float bl_mag = hypotf(bl_re, bl_im);
  741. float br_mag = hypotf(br_re, br_im);
  742. float bl_phase = atan2f(bl_im, bl_re);
  743. float br_phase = atan2f(br_im, br_re);
  744. float phase_difl = fabsf(fl_phase - bl_phase);
  745. float phase_difr = fabsf(fr_phase - br_phase);
  746. float mag_difl = (fl_mag - bl_mag) / (fl_mag + bl_mag);
  747. float mag_difr = (fr_mag - br_mag) / (fr_mag + br_mag);
  748. float mag_totall = hypotf(fl_mag, bl_mag);
  749. float mag_totalr = hypotf(fr_mag, br_mag);
  750. float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
  751. float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
  752. float xl, yl;
  753. float xr, yr;
  754. if (phase_difl > M_PI)
  755. phase_difl = 2 * M_PI - phase_difl;
  756. if (phase_difr > M_PI)
  757. phase_difr = 2 * M_PI - phase_difr;
  758. stereo_position(mag_difl, phase_difl, &xl, &yl);
  759. stereo_position(mag_difr, phase_difr, &xr, &yr);
  760. s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
  761. mag_totall, mag_totalr,
  762. fl_phase, fr_phase,
  763. bl_phase, br_phase,
  764. sl_phase, sr_phase,
  765. xl, yl, xr, yr, n);
  766. }
  767. }
  768. static int init(AVFilterContext *ctx)
  769. {
  770. AudioSurroundContext *s = ctx->priv;
  771. float overlap;
  772. int i;
  773. if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
  774. av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
  775. s->out_channel_layout_str);
  776. return AVERROR(EINVAL);
  777. }
  778. if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
  779. av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
  780. s->in_channel_layout_str);
  781. return AVERROR(EINVAL);
  782. }
  783. if (s->lowcutf >= s->highcutf) {
  784. av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
  785. s->lowcutf, s->highcutf);
  786. return AVERROR(EINVAL);
  787. }
  788. switch (s->in_channel_layout) {
  789. case AV_CH_LAYOUT_STEREO:
  790. s->filter = filter_stereo;
  791. switch (s->out_channel_layout) {
  792. case AV_CH_LAYOUT_MONO:
  793. s->upmix_stereo = upmix_1_0;
  794. break;
  795. case AV_CH_LAYOUT_STEREO:
  796. s->upmix_stereo = upmix_stereo;
  797. break;
  798. case AV_CH_LAYOUT_2POINT1:
  799. s->upmix_stereo = upmix_2_1;
  800. break;
  801. case AV_CH_LAYOUT_SURROUND:
  802. s->upmix_stereo = upmix_3_0;
  803. break;
  804. case AV_CH_LAYOUT_3POINT1:
  805. s->upmix_stereo = upmix_3_1;
  806. break;
  807. case AV_CH_LAYOUT_4POINT0:
  808. s->upmix_stereo = upmix_4_0;
  809. break;
  810. case AV_CH_LAYOUT_4POINT1:
  811. s->upmix_stereo = upmix_4_1;
  812. break;
  813. case AV_CH_LAYOUT_5POINT0_BACK:
  814. s->upmix_stereo = upmix_5_0_back;
  815. break;
  816. case AV_CH_LAYOUT_5POINT1_BACK:
  817. s->upmix_stereo = upmix_5_1_back;
  818. break;
  819. case AV_CH_LAYOUT_7POINT0:
  820. s->upmix_stereo = upmix_7_0;
  821. break;
  822. case AV_CH_LAYOUT_7POINT1:
  823. s->upmix_stereo = upmix_7_1;
  824. break;
  825. default:
  826. goto fail;
  827. }
  828. break;
  829. case AV_CH_LAYOUT_2POINT1:
  830. s->filter = filter_2_1;
  831. switch (s->out_channel_layout) {
  832. case AV_CH_LAYOUT_5POINT1_BACK:
  833. s->upmix_2_1 = upmix_5_1_back_2_1;
  834. break;
  835. default:
  836. goto fail;
  837. }
  838. break;
  839. case AV_CH_LAYOUT_SURROUND:
  840. s->filter = filter_surround;
  841. switch (s->out_channel_layout) {
  842. case AV_CH_LAYOUT_3POINT1:
  843. s->upmix_3_0 = upmix_3_1_surround;
  844. break;
  845. case AV_CH_LAYOUT_5POINT1_BACK:
  846. s->upmix_3_0 = upmix_5_1_back_surround;
  847. break;
  848. default:
  849. goto fail;
  850. }
  851. break;
  852. case AV_CH_LAYOUT_5POINT1_BACK:
  853. s->filter = filter_5_1_back;
  854. switch (s->out_channel_layout) {
  855. case AV_CH_LAYOUT_7POINT1:
  856. s->upmix_5_1 = upmix_7_1_5_1;
  857. break;
  858. default:
  859. goto fail;
  860. }
  861. break;
  862. default:
  863. fail:
  864. av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
  865. s->in_channel_layout_str, s->out_channel_layout_str);
  866. return AVERROR(EINVAL);
  867. }
  868. s->buf_size = 4096;
  869. s->pts = AV_NOPTS_VALUE;
  870. s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
  871. if (!s->window_func_lut)
  872. return AVERROR(ENOMEM);
  873. for (i = 0; i < s->buf_size; i++)
  874. s->window_func_lut[i] = sqrtf(0.5 * (1 - cosf(2 * M_PI * i / s->buf_size)) / s->buf_size);
  875. overlap = .5;
  876. s->hop_size = s->buf_size * (1. - overlap);
  877. return 0;
  878. }
  879. static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  880. {
  881. AudioSurroundContext *s = ctx->priv;
  882. const float level_in = s->level_in;
  883. float *dst;
  884. int n;
  885. memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
  886. dst = (float *)s->input->extended_data[ch];
  887. for (n = 0; n < s->buf_size; n++) {
  888. dst[n] *= s->window_func_lut[n] * level_in;
  889. }
  890. av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
  891. return 0;
  892. }
  893. static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  894. {
  895. AudioSurroundContext *s = ctx->priv;
  896. const float level_out = s->level_out;
  897. AVFrame *out = arg;
  898. float *dst, *ptr;
  899. int n;
  900. av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
  901. dst = (float *)s->output->extended_data[ch];
  902. ptr = (float *)s->overlap_buffer->extended_data[ch];
  903. memmove(s->overlap_buffer->extended_data[ch],
  904. s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
  905. s->buf_size * sizeof(float));
  906. memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
  907. 0, s->hop_size * sizeof(float));
  908. for (n = 0; n < s->buf_size; n++) {
  909. ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
  910. }
  911. ptr = (float *)s->overlap_buffer->extended_data[ch];
  912. dst = (float *)out->extended_data[ch];
  913. memcpy(dst, ptr, s->hop_size * sizeof(float));
  914. return 0;
  915. }
  916. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  917. {
  918. AVFilterContext *ctx = inlink->dst;
  919. AVFilterLink *outlink = ctx->outputs[0];
  920. AudioSurroundContext *s = ctx->priv;
  921. av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  922. in->nb_samples);
  923. if (s->pts == AV_NOPTS_VALUE)
  924. s->pts = in->pts;
  925. av_frame_free(&in);
  926. while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
  927. AVFrame *out;
  928. int ret;
  929. ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
  930. if (ret < 0)
  931. return ret;
  932. ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
  933. s->filter(ctx);
  934. out = ff_get_audio_buffer(outlink, s->hop_size);
  935. if (!out)
  936. return AVERROR(ENOMEM);
  937. ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
  938. out->pts = s->pts;
  939. if (s->pts != AV_NOPTS_VALUE)
  940. s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  941. av_audio_fifo_drain(s->fifo, s->hop_size);
  942. ret = ff_filter_frame(outlink, out);
  943. if (ret < 0)
  944. return ret;
  945. }
  946. return 0;
  947. }
  948. static av_cold void uninit(AVFilterContext *ctx)
  949. {
  950. AudioSurroundContext *s = ctx->priv;
  951. int ch;
  952. av_frame_free(&s->input);
  953. av_frame_free(&s->output);
  954. av_frame_free(&s->overlap_buffer);
  955. for (ch = 0; ch < s->nb_in_channels; ch++) {
  956. av_rdft_end(s->rdft[ch]);
  957. }
  958. for (ch = 0; ch < s->nb_out_channels; ch++) {
  959. av_rdft_end(s->irdft[ch]);
  960. }
  961. av_freep(&s->rdft);
  962. av_freep(&s->irdft);
  963. av_audio_fifo_free(s->fifo);
  964. av_freep(&s->window_func_lut);
  965. }
  966. #define OFFSET(x) offsetof(AudioSurroundContext, x)
  967. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  968. static const AVOption surround_options[] = {
  969. { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
  970. { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
  971. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  972. { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  973. { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  974. { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
  975. { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
  976. { NULL }
  977. };
  978. AVFILTER_DEFINE_CLASS(surround);
  979. static const AVFilterPad inputs[] = {
  980. {
  981. .name = "default",
  982. .type = AVMEDIA_TYPE_AUDIO,
  983. .filter_frame = filter_frame,
  984. .config_props = config_input,
  985. },
  986. { NULL }
  987. };
  988. static const AVFilterPad outputs[] = {
  989. {
  990. .name = "default",
  991. .type = AVMEDIA_TYPE_AUDIO,
  992. .config_props = config_output,
  993. },
  994. { NULL }
  995. };
  996. AVFilter ff_af_surround = {
  997. .name = "surround",
  998. .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
  999. .query_formats = query_formats,
  1000. .priv_size = sizeof(AudioSurroundContext),
  1001. .priv_class = &surround_class,
  1002. .init = init,
  1003. .uninit = uninit,
  1004. .inputs = inputs,
  1005. .outputs = outputs,
  1006. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1007. };