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.

1678 lines
64KB

  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. #include "window_func.h"
  28. typedef struct AudioSurroundContext {
  29. const AVClass *class;
  30. char *out_channel_layout_str;
  31. char *in_channel_layout_str;
  32. float level_in;
  33. float level_out;
  34. float fc_in;
  35. float fc_out;
  36. float lfe_in;
  37. float lfe_out;
  38. int lfe_mode;
  39. int win_func;
  40. float overlap;
  41. float all_x;
  42. float all_y;
  43. float fc_x;
  44. float fl_x;
  45. float fr_x;
  46. float bl_x;
  47. float br_x;
  48. float sl_x;
  49. float sr_x;
  50. float bc_x;
  51. float fc_y;
  52. float fl_y;
  53. float fr_y;
  54. float bl_y;
  55. float br_y;
  56. float sl_y;
  57. float sr_y;
  58. float bc_y;
  59. float *input_levels;
  60. float *output_levels;
  61. int output_lfe;
  62. int lowcutf;
  63. int highcutf;
  64. float lowcut;
  65. float highcut;
  66. uint64_t out_channel_layout;
  67. uint64_t in_channel_layout;
  68. int nb_in_channels;
  69. int nb_out_channels;
  70. AVFrame *input;
  71. AVFrame *output;
  72. AVFrame *overlap_buffer;
  73. int buf_size;
  74. int hop_size;
  75. AVAudioFifo *fifo;
  76. RDFTContext **rdft, **irdft;
  77. float *window_func_lut;
  78. int64_t pts;
  79. void (*filter)(AVFilterContext *ctx);
  80. void (*upmix_stereo)(AVFilterContext *ctx,
  81. float l_phase,
  82. float r_phase,
  83. float c_phase,
  84. float mag_total,
  85. float x, float y,
  86. int n);
  87. void (*upmix_2_1)(AVFilterContext *ctx,
  88. float l_phase,
  89. float r_phase,
  90. float c_phase,
  91. float mag_total,
  92. float lfe_im,
  93. float lfe_re,
  94. float x, float y,
  95. int n);
  96. void (*upmix_3_0)(AVFilterContext *ctx,
  97. float l_phase,
  98. float r_phase,
  99. float c_mag,
  100. float c_phase,
  101. float mag_total,
  102. float x, float y,
  103. int n);
  104. void (*upmix_5_0)(AVFilterContext *ctx,
  105. float c_re, float c_im,
  106. float mag_totall, float mag_totalr,
  107. float fl_phase, float fr_phase,
  108. float bl_phase, float br_phase,
  109. float sl_phase, float sr_phase,
  110. float xl, float yl,
  111. float xr, float yr,
  112. int n);
  113. void (*upmix_5_1)(AVFilterContext *ctx,
  114. float c_re, float c_im,
  115. float lfe_re, float lfe_im,
  116. float mag_totall, float mag_totalr,
  117. float fl_phase, float fr_phase,
  118. float bl_phase, float br_phase,
  119. float sl_phase, float sr_phase,
  120. float xl, float yl,
  121. float xr, float yr,
  122. int n);
  123. } AudioSurroundContext;
  124. static int query_formats(AVFilterContext *ctx)
  125. {
  126. AudioSurroundContext *s = ctx->priv;
  127. AVFilterFormats *formats = NULL;
  128. AVFilterChannelLayouts *layouts = NULL;
  129. int ret;
  130. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
  131. if (ret)
  132. return ret;
  133. ret = ff_set_common_formats(ctx, formats);
  134. if (ret)
  135. return ret;
  136. layouts = NULL;
  137. ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
  138. if (ret)
  139. return ret;
  140. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  141. if (ret)
  142. return ret;
  143. layouts = NULL;
  144. ret = ff_add_channel_layout(&layouts, s->in_channel_layout);
  145. if (ret)
  146. return ret;
  147. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  148. if (ret)
  149. return ret;
  150. formats = ff_all_samplerates();
  151. if (!formats)
  152. return AVERROR(ENOMEM);
  153. return ff_set_common_samplerates(ctx, formats);
  154. }
  155. static int config_input(AVFilterLink *inlink)
  156. {
  157. AVFilterContext *ctx = inlink->dst;
  158. AudioSurroundContext *s = ctx->priv;
  159. int ch;
  160. s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
  161. if (!s->rdft)
  162. return AVERROR(ENOMEM);
  163. for (ch = 0; ch < inlink->channels; ch++) {
  164. s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
  165. if (!s->rdft[ch])
  166. return AVERROR(ENOMEM);
  167. }
  168. s->nb_in_channels = inlink->channels;
  169. s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
  170. if (!s->input_levels)
  171. return AVERROR(ENOMEM);
  172. for (ch = 0; ch < s->nb_in_channels; ch++)
  173. s->input_levels[ch] = s->level_in;
  174. ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_FRONT_CENTER);
  175. if (ch >= 0)
  176. s->input_levels[ch] *= s->fc_in;
  177. ch = av_get_channel_layout_channel_index(inlink->channel_layout, AV_CH_LOW_FREQUENCY);
  178. if (ch >= 0)
  179. s->input_levels[ch] *= s->lfe_in;
  180. s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
  181. if (!s->input)
  182. return AVERROR(ENOMEM);
  183. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
  184. if (!s->fifo)
  185. return AVERROR(ENOMEM);
  186. s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
  187. s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
  188. return 0;
  189. }
  190. static int config_output(AVFilterLink *outlink)
  191. {
  192. AVFilterContext *ctx = outlink->src;
  193. AudioSurroundContext *s = ctx->priv;
  194. int ch;
  195. s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
  196. if (!s->irdft)
  197. return AVERROR(ENOMEM);
  198. for (ch = 0; ch < outlink->channels; ch++) {
  199. s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
  200. if (!s->irdft[ch])
  201. return AVERROR(ENOMEM);
  202. }
  203. s->nb_out_channels = outlink->channels;
  204. s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
  205. if (!s->output_levels)
  206. return AVERROR(ENOMEM);
  207. for (ch = 0; ch < s->nb_out_channels; ch++)
  208. s->output_levels[ch] = s->level_out;
  209. ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_FRONT_CENTER);
  210. if (ch >= 0)
  211. s->output_levels[ch] *= s->fc_out;
  212. ch = av_get_channel_layout_channel_index(outlink->channel_layout, AV_CH_LOW_FREQUENCY);
  213. if (ch >= 0)
  214. s->output_levels[ch] *= s->lfe_out;
  215. s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
  216. s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
  217. if (!s->overlap_buffer || !s->output)
  218. return AVERROR(ENOMEM);
  219. return 0;
  220. }
  221. static void stereo_position(float a, float p, float *x, float *y)
  222. {
  223. *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
  224. *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
  225. }
  226. static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
  227. float *lfe_mag, float *mag_total, int lfe_mode)
  228. {
  229. if (output_lfe && n < highcut) {
  230. *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
  231. *lfe_mag *= *mag_total;
  232. if (lfe_mode)
  233. *mag_total -= *lfe_mag;
  234. } else {
  235. *lfe_mag = 0.f;
  236. }
  237. }
  238. static void upmix_1_0(AVFilterContext *ctx,
  239. float l_phase,
  240. float r_phase,
  241. float c_phase,
  242. float mag_total,
  243. float x, float y,
  244. int n)
  245. {
  246. AudioSurroundContext *s = ctx->priv;
  247. float mag, *dst;
  248. dst = (float *)s->output->extended_data[0];
  249. mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  250. dst[2 * n ] = mag * cosf(c_phase);
  251. dst[2 * n + 1] = mag * sinf(c_phase);
  252. }
  253. static void upmix_stereo(AVFilterContext *ctx,
  254. float l_phase,
  255. float r_phase,
  256. float c_phase,
  257. float mag_total,
  258. float x, float y,
  259. int n)
  260. {
  261. AudioSurroundContext *s = ctx->priv;
  262. float l_mag, r_mag, *dstl, *dstr;
  263. dstl = (float *)s->output->extended_data[0];
  264. dstr = (float *)s->output->extended_data[1];
  265. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  266. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  267. dstl[2 * n ] = l_mag * cosf(l_phase);
  268. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  269. dstr[2 * n ] = r_mag * cosf(r_phase);
  270. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  271. }
  272. static void upmix_2_1(AVFilterContext *ctx,
  273. float l_phase,
  274. float r_phase,
  275. float c_phase,
  276. float mag_total,
  277. float x, float y,
  278. int n)
  279. {
  280. AudioSurroundContext *s = ctx->priv;
  281. float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
  282. dstl = (float *)s->output->extended_data[0];
  283. dstr = (float *)s->output->extended_data[1];
  284. dstlfe = (float *)s->output->extended_data[2];
  285. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  286. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  287. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  288. dstl[2 * n ] = l_mag * cosf(l_phase);
  289. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  290. dstr[2 * n ] = r_mag * cosf(r_phase);
  291. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  292. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  293. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  294. }
  295. static void upmix_3_0(AVFilterContext *ctx,
  296. float l_phase,
  297. float r_phase,
  298. float c_phase,
  299. float mag_total,
  300. float x, float y,
  301. int n)
  302. {
  303. AudioSurroundContext *s = ctx->priv;
  304. float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
  305. dstl = (float *)s->output->extended_data[0];
  306. dstr = (float *)s->output->extended_data[1];
  307. dstc = (float *)s->output->extended_data[2];
  308. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  309. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  310. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  311. dstl[2 * n ] = l_mag * cosf(l_phase);
  312. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  313. dstr[2 * n ] = r_mag * cosf(r_phase);
  314. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  315. dstc[2 * n ] = c_mag * cosf(c_phase);
  316. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  317. }
  318. static void upmix_3_1(AVFilterContext *ctx,
  319. float l_phase,
  320. float r_phase,
  321. float c_phase,
  322. float mag_total,
  323. float x, float y,
  324. int n)
  325. {
  326. AudioSurroundContext *s = ctx->priv;
  327. float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
  328. dstl = (float *)s->output->extended_data[0];
  329. dstr = (float *)s->output->extended_data[1];
  330. dstc = (float *)s->output->extended_data[2];
  331. dstlfe = (float *)s->output->extended_data[3];
  332. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  333. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  334. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  335. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  336. dstl[2 * n ] = l_mag * cosf(l_phase);
  337. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  338. dstr[2 * n ] = r_mag * cosf(r_phase);
  339. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  340. dstc[2 * n ] = c_mag * cosf(c_phase);
  341. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  342. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  343. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  344. }
  345. static void upmix_3_1_surround(AVFilterContext *ctx,
  346. float l_phase,
  347. float r_phase,
  348. float c_phase,
  349. float c_mag,
  350. float mag_total,
  351. float x, float y,
  352. int n)
  353. {
  354. AudioSurroundContext *s = ctx->priv;
  355. float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
  356. dstl = (float *)s->output->extended_data[0];
  357. dstr = (float *)s->output->extended_data[1];
  358. dstc = (float *)s->output->extended_data[2];
  359. dstlfe = (float *)s->output->extended_data[3];
  360. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
  361. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  362. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  363. dstl[2 * n ] = l_mag * cosf(l_phase);
  364. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  365. dstr[2 * n ] = r_mag * cosf(r_phase);
  366. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  367. dstc[2 * n ] = c_mag * cosf(c_phase);
  368. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  369. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  370. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  371. }
  372. static void upmix_4_0(AVFilterContext *ctx,
  373. float l_phase,
  374. float r_phase,
  375. float c_phase,
  376. float mag_total,
  377. float x, float y,
  378. int n)
  379. {
  380. AudioSurroundContext *s = ctx->priv;
  381. float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
  382. dstl = (float *)s->output->extended_data[0];
  383. dstr = (float *)s->output->extended_data[1];
  384. dstc = (float *)s->output->extended_data[2];
  385. dstb = (float *)s->output->extended_data[3];
  386. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  387. b_mag = powf(1.f - fabsf(x), s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
  388. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  389. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * 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. dstb[2 * n ] = b_mag * cosf(c_phase);
  397. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  398. }
  399. static void upmix_4_1(AVFilterContext *ctx,
  400. float l_phase,
  401. float r_phase,
  402. float c_phase,
  403. float mag_total,
  404. float x, float y,
  405. int n)
  406. {
  407. AudioSurroundContext *s = ctx->priv;
  408. float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
  409. dstl = (float *)s->output->extended_data[0];
  410. dstr = (float *)s->output->extended_data[1];
  411. dstc = (float *)s->output->extended_data[2];
  412. dstlfe = (float *)s->output->extended_data[3];
  413. dstb = (float *)s->output->extended_data[4];
  414. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  415. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  416. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  417. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  418. b_mag = powf(1.f - fabsf(x), s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
  419. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  420. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  421. dstl[2 * n ] = l_mag * cosf(l_phase);
  422. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  423. dstr[2 * n ] = r_mag * cosf(r_phase);
  424. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  425. dstc[2 * n ] = c_mag * cosf(c_phase);
  426. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  427. dstb[2 * n ] = b_mag * cosf(c_phase);
  428. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  429. }
  430. static void upmix_5_0_back(AVFilterContext *ctx,
  431. float l_phase,
  432. float r_phase,
  433. float c_phase,
  434. float mag_total,
  435. float x, float y,
  436. int n)
  437. {
  438. AudioSurroundContext *s = ctx->priv;
  439. float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
  440. dstl = (float *)s->output->extended_data[0];
  441. dstr = (float *)s->output->extended_data[1];
  442. dstc = (float *)s->output->extended_data[2];
  443. dstls = (float *)s->output->extended_data[3];
  444. dstrs = (float *)s->output->extended_data[4];
  445. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  446. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  447. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  448. ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  449. rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  450. dstl[2 * n ] = l_mag * cosf(l_phase);
  451. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  452. dstr[2 * n ] = r_mag * cosf(r_phase);
  453. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  454. dstc[2 * n ] = c_mag * cosf(c_phase);
  455. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  456. dstls[2 * n ] = ls_mag * cosf(l_phase);
  457. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  458. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  459. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  460. }
  461. static void upmix_5_1_back(AVFilterContext *ctx,
  462. float l_phase,
  463. float r_phase,
  464. float c_phase,
  465. float mag_total,
  466. float x, float y,
  467. int n)
  468. {
  469. AudioSurroundContext *s = ctx->priv;
  470. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
  471. dstl = (float *)s->output->extended_data[0];
  472. dstr = (float *)s->output->extended_data[1];
  473. dstc = (float *)s->output->extended_data[2];
  474. dstlfe = (float *)s->output->extended_data[3];
  475. dstls = (float *)s->output->extended_data[4];
  476. dstrs = (float *)s->output->extended_data[5];
  477. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  478. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  479. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  480. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  481. ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  482. rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  483. dstl[2 * n ] = l_mag * cosf(l_phase);
  484. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  485. dstr[2 * n ] = r_mag * cosf(r_phase);
  486. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  487. dstc[2 * n ] = c_mag * cosf(c_phase);
  488. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  489. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  490. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  491. dstls[2 * n ] = ls_mag * cosf(l_phase);
  492. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  493. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  494. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  495. }
  496. static void upmix_6_0(AVFilterContext *ctx,
  497. float l_phase,
  498. float r_phase,
  499. float c_phase,
  500. float mag_total,
  501. float x, float y,
  502. int n)
  503. {
  504. AudioSurroundContext *s = ctx->priv;
  505. float l_mag, r_mag, ls_mag, rs_mag, c_mag, b_mag, *dstc, *dstb, *dstl, *dstr, *dstls, *dstrs;
  506. dstl = (float *)s->output->extended_data[0];
  507. dstr = (float *)s->output->extended_data[1];
  508. dstc = (float *)s->output->extended_data[2];
  509. dstb = (float *)s->output->extended_data[3];
  510. dstls = (float *)s->output->extended_data[4];
  511. dstrs = (float *)s->output->extended_data[5];
  512. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  513. b_mag = powf(1.f - fabsf(x), s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
  514. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  515. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  516. ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  517. rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  518. dstl[2 * n ] = l_mag * cosf(l_phase);
  519. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  520. dstr[2 * n ] = r_mag * cosf(r_phase);
  521. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  522. dstc[2 * n ] = c_mag * cosf(c_phase);
  523. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  524. dstls[2 * n ] = ls_mag * cosf(l_phase);
  525. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  526. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  527. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  528. dstb[2 * n ] = b_mag * cosf(c_phase);
  529. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  530. }
  531. static void upmix_6_1(AVFilterContext *ctx,
  532. float l_phase,
  533. float r_phase,
  534. float c_phase,
  535. float mag_total,
  536. float x, float y,
  537. int n)
  538. {
  539. AudioSurroundContext *s = ctx->priv;
  540. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, b_mag, *dstc, *dstb, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
  541. dstl = (float *)s->output->extended_data[0];
  542. dstr = (float *)s->output->extended_data[1];
  543. dstc = (float *)s->output->extended_data[2];
  544. dstlfe = (float *)s->output->extended_data[3];
  545. dstb = (float *)s->output->extended_data[4];
  546. dstls = (float *)s->output->extended_data[5];
  547. dstrs = (float *)s->output->extended_data[6];
  548. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  549. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  550. b_mag = powf(1.f - fabsf(x), s->bc_x) * powf((1.f - y) * .5f, s->bc_y) * mag_total;
  551. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  552. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  553. ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  554. rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  555. dstl[2 * n ] = l_mag * cosf(l_phase);
  556. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  557. dstr[2 * n ] = r_mag * cosf(r_phase);
  558. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  559. dstc[2 * n ] = c_mag * cosf(c_phase);
  560. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  561. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  562. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  563. dstls[2 * n ] = ls_mag * cosf(l_phase);
  564. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  565. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  566. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  567. dstb[2 * n ] = b_mag * cosf(c_phase);
  568. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  569. }
  570. static void upmix_5_1_back_surround(AVFilterContext *ctx,
  571. float l_phase,
  572. float r_phase,
  573. float c_phase,
  574. float c_mag,
  575. float mag_total,
  576. float x, float y,
  577. int n)
  578. {
  579. AudioSurroundContext *s = ctx->priv;
  580. float lfe_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
  581. float ls_mag, rs_mag, *dstls, *dstrs;
  582. dstl = (float *)s->output->extended_data[0];
  583. dstr = (float *)s->output->extended_data[1];
  584. dstc = (float *)s->output->extended_data[2];
  585. dstlfe = (float *)s->output->extended_data[3];
  586. dstls = (float *)s->output->extended_data[4];
  587. dstrs = (float *)s->output->extended_data[5];
  588. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &c_mag, s->lfe_mode);
  589. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  590. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  591. ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  592. rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  593. dstl[2 * n ] = l_mag * cosf(l_phase);
  594. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  595. dstr[2 * n ] = r_mag * cosf(r_phase);
  596. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  597. dstc[2 * n ] = c_mag * cosf(c_phase);
  598. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  599. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  600. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  601. dstls[2 * n ] = ls_mag * cosf(l_phase);
  602. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  603. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  604. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  605. }
  606. static void upmix_5_1_back_2_1(AVFilterContext *ctx,
  607. float l_phase,
  608. float r_phase,
  609. float c_phase,
  610. float mag_total,
  611. float lfe_re,
  612. float lfe_im,
  613. float x, float y,
  614. int n)
  615. {
  616. AudioSurroundContext *s = ctx->priv;
  617. float c_mag, l_mag, r_mag, *dstc, *dstl, *dstr, *dstlfe;
  618. float ls_mag, rs_mag, *dstls, *dstrs;
  619. dstl = (float *)s->output->extended_data[0];
  620. dstr = (float *)s->output->extended_data[1];
  621. dstc = (float *)s->output->extended_data[2];
  622. dstlfe = (float *)s->output->extended_data[3];
  623. dstls = (float *)s->output->extended_data[4];
  624. dstrs = (float *)s->output->extended_data[5];
  625. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  626. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  627. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  628. ls_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  629. rs_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  630. dstl[2 * n ] = l_mag * cosf(l_phase);
  631. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  632. dstr[2 * n ] = r_mag * cosf(r_phase);
  633. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  634. dstc[2 * n ] = c_mag * cosf(c_phase);
  635. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  636. dstlfe[2 * n ] = lfe_re;
  637. dstlfe[2 * n + 1] = lfe_im;
  638. dstls[2 * n ] = ls_mag * cosf(l_phase);
  639. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  640. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  641. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  642. }
  643. static void upmix_7_0(AVFilterContext *ctx,
  644. float l_phase,
  645. float r_phase,
  646. float c_phase,
  647. float mag_total,
  648. float x, float y,
  649. int n)
  650. {
  651. float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
  652. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
  653. AudioSurroundContext *s = ctx->priv;
  654. dstl = (float *)s->output->extended_data[0];
  655. dstr = (float *)s->output->extended_data[1];
  656. dstc = (float *)s->output->extended_data[2];
  657. dstlb = (float *)s->output->extended_data[3];
  658. dstrb = (float *)s->output->extended_data[4];
  659. dstls = (float *)s->output->extended_data[5];
  660. dstrs = (float *)s->output->extended_data[6];
  661. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  662. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  663. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  664. lb_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  665. rb_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  666. ls_mag = powf(.5f * ( x + 1.f), s->sl_x) * powf(1.f - fabsf(y), s->sl_y) * mag_total;
  667. rs_mag = powf(.5f * (-x + 1.f), s->sr_x) * powf(1.f - fabsf(y), s->sr_y) * mag_total;
  668. dstl[2 * n ] = l_mag * cosf(l_phase);
  669. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  670. dstr[2 * n ] = r_mag * cosf(r_phase);
  671. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  672. dstc[2 * n ] = c_mag * cosf(c_phase);
  673. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  674. dstlb[2 * n ] = lb_mag * cosf(l_phase);
  675. dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
  676. dstrb[2 * n ] = rb_mag * cosf(r_phase);
  677. dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
  678. dstls[2 * n ] = ls_mag * cosf(l_phase);
  679. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  680. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  681. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  682. }
  683. static void upmix_7_1(AVFilterContext *ctx,
  684. float l_phase,
  685. float r_phase,
  686. float c_phase,
  687. float mag_total,
  688. float x, float y,
  689. int n)
  690. {
  691. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
  692. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  693. AudioSurroundContext *s = ctx->priv;
  694. dstl = (float *)s->output->extended_data[0];
  695. dstr = (float *)s->output->extended_data[1];
  696. dstc = (float *)s->output->extended_data[2];
  697. dstlfe = (float *)s->output->extended_data[3];
  698. dstlb = (float *)s->output->extended_data[4];
  699. dstrb = (float *)s->output->extended_data[5];
  700. dstls = (float *)s->output->extended_data[6];
  701. dstrs = (float *)s->output->extended_data[7];
  702. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  703. c_mag = powf(1.f - fabsf(x), s->fc_x) * powf((y + 1.f) * .5f, s->fc_y) * mag_total;
  704. l_mag = powf(.5f * ( x + 1.f), s->fl_x) * powf((y + 1.f) * .5f, s->fl_y) * mag_total;
  705. r_mag = powf(.5f * (-x + 1.f), s->fr_x) * powf((y + 1.f) * .5f, s->fr_y) * mag_total;
  706. lb_mag = powf(.5f * ( x + 1.f), s->bl_x) * powf(1.f - ((y + 1.f) * .5f), s->bl_y) * mag_total;
  707. rb_mag = powf(.5f * (-x + 1.f), s->br_x) * powf(1.f - ((y + 1.f) * .5f), s->br_y) * mag_total;
  708. ls_mag = powf(.5f * ( x + 1.f), s->sl_x) * powf(1.f - fabsf(y), s->sl_y) * mag_total;
  709. rs_mag = powf(.5f * (-x + 1.f), s->sr_x) * powf(1.f - fabsf(y), s->sr_y) * mag_total;
  710. dstl[2 * n ] = l_mag * cosf(l_phase);
  711. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  712. dstr[2 * n ] = r_mag * cosf(r_phase);
  713. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  714. dstc[2 * n ] = c_mag * cosf(c_phase);
  715. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  716. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  717. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  718. dstlb[2 * n ] = lb_mag * cosf(l_phase);
  719. dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
  720. dstrb[2 * n ] = rb_mag * cosf(r_phase);
  721. dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
  722. dstls[2 * n ] = ls_mag * cosf(l_phase);
  723. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  724. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  725. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  726. }
  727. static void upmix_7_1_5_0_side(AVFilterContext *ctx,
  728. float c_re, float c_im,
  729. float mag_totall, float mag_totalr,
  730. float fl_phase, float fr_phase,
  731. float bl_phase, float br_phase,
  732. float sl_phase, float sr_phase,
  733. float xl, float yl,
  734. float xr, float yr,
  735. int n)
  736. {
  737. float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
  738. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  739. float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5;
  740. AudioSurroundContext *s = ctx->priv;
  741. dstl = (float *)s->output->extended_data[0];
  742. dstr = (float *)s->output->extended_data[1];
  743. dstc = (float *)s->output->extended_data[2];
  744. dstlfe = (float *)s->output->extended_data[3];
  745. dstlb = (float *)s->output->extended_data[4];
  746. dstrb = (float *)s->output->extended_data[5];
  747. dstls = (float *)s->output->extended_data[6];
  748. dstrs = (float *)s->output->extended_data[7];
  749. c_phase = atan2f(c_im, c_re);
  750. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total, s->lfe_mode);
  751. fl_mag = powf(.5f * (xl + 1.f), s->fl_x) * powf((yl + 1.f) * .5f, s->fl_y) * mag_totall;
  752. fr_mag = powf(.5f * (xr + 1.f), s->fr_x) * powf((yr + 1.f) * .5f, s->fr_y) * mag_totalr;
  753. lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall;
  754. rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr;
  755. ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall;
  756. rs_mag = powf(1.f - fabsf(xr), s->sr_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr;
  757. dstl[2 * n ] = fl_mag * cosf(fl_phase);
  758. dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
  759. dstr[2 * n ] = fr_mag * cosf(fr_phase);
  760. dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
  761. dstc[2 * n ] = c_re;
  762. dstc[2 * n + 1] = c_im;
  763. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  764. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  765. dstlb[2 * n ] = lb_mag * cosf(bl_phase);
  766. dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
  767. dstrb[2 * n ] = rb_mag * cosf(br_phase);
  768. dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
  769. dstls[2 * n ] = ls_mag * cosf(sl_phase);
  770. dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
  771. dstrs[2 * n ] = rs_mag * cosf(sr_phase);
  772. dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
  773. }
  774. static void upmix_7_1_5_1(AVFilterContext *ctx,
  775. float c_re, float c_im,
  776. float lfe_re, float lfe_im,
  777. float mag_totall, float mag_totalr,
  778. float fl_phase, float fr_phase,
  779. float bl_phase, float br_phase,
  780. float sl_phase, float sr_phase,
  781. float xl, float yl,
  782. float xr, float yr,
  783. int n)
  784. {
  785. float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
  786. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  787. AudioSurroundContext *s = ctx->priv;
  788. dstl = (float *)s->output->extended_data[0];
  789. dstr = (float *)s->output->extended_data[1];
  790. dstc = (float *)s->output->extended_data[2];
  791. dstlfe = (float *)s->output->extended_data[3];
  792. dstlb = (float *)s->output->extended_data[4];
  793. dstrb = (float *)s->output->extended_data[5];
  794. dstls = (float *)s->output->extended_data[6];
  795. dstrs = (float *)s->output->extended_data[7];
  796. fl_mag = powf(.5f * (xl + 1.f), s->fl_x) * powf((yl + 1.f) * .5f, s->fl_y) * mag_totall;
  797. fr_mag = powf(.5f * (xr + 1.f), s->fr_x) * powf((yr + 1.f) * .5f, s->fr_y) * mag_totalr;
  798. lb_mag = powf(.5f * (-xl + 1.f), s->bl_x) * powf((yl + 1.f) * .5f, s->bl_y) * mag_totall;
  799. rb_mag = powf(.5f * (-xr + 1.f), s->br_x) * powf((yr + 1.f) * .5f, s->br_y) * mag_totalr;
  800. ls_mag = powf(1.f - fabsf(xl), s->sl_x) * powf((yl + 1.f) * .5f, s->sl_y) * mag_totall;
  801. rs_mag = powf(1.f - fabsf(xr), s->sl_x) * powf((yr + 1.f) * .5f, s->sr_y) * mag_totalr;
  802. dstl[2 * n ] = fl_mag * cosf(fl_phase);
  803. dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
  804. dstr[2 * n ] = fr_mag * cosf(fr_phase);
  805. dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
  806. dstc[2 * n ] = c_re;
  807. dstc[2 * n + 1] = c_im;
  808. dstlfe[2 * n ] = lfe_re;
  809. dstlfe[2 * n + 1] = lfe_im;
  810. dstlb[2 * n ] = lb_mag * cosf(bl_phase);
  811. dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
  812. dstrb[2 * n ] = rb_mag * cosf(br_phase);
  813. dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
  814. dstls[2 * n ] = ls_mag * cosf(sl_phase);
  815. dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
  816. dstrs[2 * n ] = rs_mag * cosf(sr_phase);
  817. dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
  818. }
  819. static void filter_stereo(AVFilterContext *ctx)
  820. {
  821. AudioSurroundContext *s = ctx->priv;
  822. float *srcl, *srcr;
  823. int n;
  824. srcl = (float *)s->input->extended_data[0];
  825. srcr = (float *)s->input->extended_data[1];
  826. for (n = 0; n < s->buf_size; n++) {
  827. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  828. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  829. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  830. float l_mag = hypotf(l_re, l_im);
  831. float r_mag = hypotf(r_re, r_im);
  832. float l_phase = atan2f(l_im, l_re);
  833. float r_phase = atan2f(r_im, r_re);
  834. float phase_dif = fabsf(l_phase - r_phase);
  835. float mag_sum = l_mag + r_mag;
  836. float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
  837. float mag_total = hypotf(l_mag, r_mag);
  838. float x, y;
  839. if (phase_dif > M_PI)
  840. phase_dif = 2 * M_PI - phase_dif;
  841. stereo_position(mag_dif, phase_dif, &x, &y);
  842. s->upmix_stereo(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
  843. }
  844. }
  845. static void filter_surround(AVFilterContext *ctx)
  846. {
  847. AudioSurroundContext *s = ctx->priv;
  848. float *srcl, *srcr, *srcc;
  849. int n;
  850. srcl = (float *)s->input->extended_data[0];
  851. srcr = (float *)s->input->extended_data[1];
  852. srcc = (float *)s->input->extended_data[2];
  853. for (n = 0; n < s->buf_size; n++) {
  854. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  855. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  856. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  857. float c_mag = hypotf(c_re, c_im);
  858. float c_phase = atan2f(c_im, c_re);
  859. float l_mag = hypotf(l_re, l_im);
  860. float r_mag = hypotf(r_re, r_im);
  861. float l_phase = atan2f(l_im, l_re);
  862. float r_phase = atan2f(r_im, r_re);
  863. float phase_dif = fabsf(l_phase - r_phase);
  864. float mag_sum = l_mag + r_mag;
  865. float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
  866. float mag_total = hypotf(l_mag, r_mag);
  867. float x, y;
  868. if (phase_dif > M_PI)
  869. phase_dif = 2 * M_PI - phase_dif;
  870. stereo_position(mag_dif, phase_dif, &x, &y);
  871. s->upmix_3_0(ctx, l_phase, r_phase, c_phase, c_mag, mag_total, x, y, n);
  872. }
  873. }
  874. static void filter_2_1(AVFilterContext *ctx)
  875. {
  876. AudioSurroundContext *s = ctx->priv;
  877. float *srcl, *srcr, *srclfe;
  878. int n;
  879. srcl = (float *)s->input->extended_data[0];
  880. srcr = (float *)s->input->extended_data[1];
  881. srclfe = (float *)s->input->extended_data[2];
  882. for (n = 0; n < s->buf_size; n++) {
  883. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  884. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  885. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  886. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  887. float l_mag = hypotf(l_re, l_im);
  888. float r_mag = hypotf(r_re, r_im);
  889. float l_phase = atan2f(l_im, l_re);
  890. float r_phase = atan2f(r_im, r_re);
  891. float phase_dif = fabsf(l_phase - r_phase);
  892. float mag_sum = l_mag + r_mag;
  893. float mag_dif = mag_sum < 0.000001 ? 0.f : (l_mag - r_mag) / mag_sum;
  894. float mag_total = hypotf(l_mag, r_mag);
  895. float x, y;
  896. if (phase_dif > M_PI)
  897. phase_dif = 2 * M_PI - phase_dif;
  898. stereo_position(mag_dif, phase_dif, &x, &y);
  899. s->upmix_2_1(ctx, l_phase, r_phase, c_phase, mag_total, lfe_re, lfe_im, x, y, n);
  900. }
  901. }
  902. static void filter_5_0_side(AVFilterContext *ctx)
  903. {
  904. AudioSurroundContext *s = ctx->priv;
  905. float *srcl, *srcr, *srcc, *srcsl, *srcsr;
  906. int n;
  907. srcl = (float *)s->input->extended_data[0];
  908. srcr = (float *)s->input->extended_data[1];
  909. srcc = (float *)s->input->extended_data[2];
  910. srcsl = (float *)s->input->extended_data[3];
  911. srcsr = (float *)s->input->extended_data[4];
  912. for (n = 0; n < s->buf_size; n++) {
  913. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  914. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  915. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  916. float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
  917. float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
  918. float fl_mag = hypotf(fl_re, fl_im);
  919. float fr_mag = hypotf(fr_re, fr_im);
  920. float fl_phase = atan2f(fl_im, fl_re);
  921. float fr_phase = atan2f(fr_im, fr_re);
  922. float sl_mag = hypotf(sl_re, sl_im);
  923. float sr_mag = hypotf(sr_re, sr_im);
  924. float sl_phase = atan2f(sl_im, sl_re);
  925. float sr_phase = atan2f(sr_im, sr_re);
  926. float phase_difl = fabsf(fl_phase - sl_phase);
  927. float phase_difr = fabsf(fr_phase - sr_phase);
  928. float magl_sum = fl_mag + sl_mag;
  929. float magr_sum = fr_mag + sr_mag;
  930. float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
  931. float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
  932. float mag_totall = hypotf(fl_mag, sl_mag);
  933. float mag_totalr = hypotf(fr_mag, sr_mag);
  934. float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
  935. float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
  936. float xl, yl;
  937. float xr, yr;
  938. if (phase_difl > M_PI)
  939. phase_difl = 2 * M_PI - phase_difl;
  940. if (phase_difr > M_PI)
  941. phase_difr = 2 * M_PI - phase_difr;
  942. stereo_position(mag_difl, phase_difl, &xl, &yl);
  943. stereo_position(mag_difr, phase_difr, &xr, &yr);
  944. s->upmix_5_0(ctx, c_re, c_im,
  945. mag_totall, mag_totalr,
  946. fl_phase, fr_phase,
  947. bl_phase, br_phase,
  948. sl_phase, sr_phase,
  949. xl, yl, xr, yr, n);
  950. }
  951. }
  952. static void filter_5_1_side(AVFilterContext *ctx)
  953. {
  954. AudioSurroundContext *s = ctx->priv;
  955. float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
  956. int n;
  957. srcl = (float *)s->input->extended_data[0];
  958. srcr = (float *)s->input->extended_data[1];
  959. srcc = (float *)s->input->extended_data[2];
  960. srclfe = (float *)s->input->extended_data[3];
  961. srcsl = (float *)s->input->extended_data[4];
  962. srcsr = (float *)s->input->extended_data[5];
  963. for (n = 0; n < s->buf_size; n++) {
  964. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  965. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  966. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  967. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  968. float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
  969. float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
  970. float fl_mag = hypotf(fl_re, fl_im);
  971. float fr_mag = hypotf(fr_re, fr_im);
  972. float fl_phase = atan2f(fl_im, fl_re);
  973. float fr_phase = atan2f(fr_im, fr_re);
  974. float sl_mag = hypotf(sl_re, sl_im);
  975. float sr_mag = hypotf(sr_re, sr_im);
  976. float sl_phase = atan2f(sl_im, sl_re);
  977. float sr_phase = atan2f(sr_im, sr_re);
  978. float phase_difl = fabsf(fl_phase - sl_phase);
  979. float phase_difr = fabsf(fr_phase - sr_phase);
  980. float magl_sum = fl_mag + sl_mag;
  981. float magr_sum = fr_mag + sr_mag;
  982. float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - sl_mag) / magl_sum;
  983. float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - sr_mag) / magr_sum;
  984. float mag_totall = hypotf(fl_mag, sl_mag);
  985. float mag_totalr = hypotf(fr_mag, sr_mag);
  986. float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
  987. float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
  988. float xl, yl;
  989. float xr, yr;
  990. if (phase_difl > M_PI)
  991. phase_difl = 2 * M_PI - phase_difl;
  992. if (phase_difr > M_PI)
  993. phase_difr = 2 * M_PI - phase_difr;
  994. stereo_position(mag_difl, phase_difl, &xl, &yl);
  995. stereo_position(mag_difr, phase_difr, &xr, &yr);
  996. s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
  997. mag_totall, mag_totalr,
  998. fl_phase, fr_phase,
  999. bl_phase, br_phase,
  1000. sl_phase, sr_phase,
  1001. xl, yl, xr, yr, n);
  1002. }
  1003. }
  1004. static void filter_5_1_back(AVFilterContext *ctx)
  1005. {
  1006. AudioSurroundContext *s = ctx->priv;
  1007. float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
  1008. int n;
  1009. srcl = (float *)s->input->extended_data[0];
  1010. srcr = (float *)s->input->extended_data[1];
  1011. srcc = (float *)s->input->extended_data[2];
  1012. srclfe = (float *)s->input->extended_data[3];
  1013. srcbl = (float *)s->input->extended_data[4];
  1014. srcbr = (float *)s->input->extended_data[5];
  1015. for (n = 0; n < s->buf_size; n++) {
  1016. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  1017. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  1018. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  1019. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  1020. float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
  1021. float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
  1022. float fl_mag = hypotf(fl_re, fl_im);
  1023. float fr_mag = hypotf(fr_re, fr_im);
  1024. float fl_phase = atan2f(fl_im, fl_re);
  1025. float fr_phase = atan2f(fr_im, fr_re);
  1026. float bl_mag = hypotf(bl_re, bl_im);
  1027. float br_mag = hypotf(br_re, br_im);
  1028. float bl_phase = atan2f(bl_im, bl_re);
  1029. float br_phase = atan2f(br_im, br_re);
  1030. float phase_difl = fabsf(fl_phase - bl_phase);
  1031. float phase_difr = fabsf(fr_phase - br_phase);
  1032. float magl_sum = fl_mag + bl_mag;
  1033. float magr_sum = fr_mag + br_mag;
  1034. float mag_difl = magl_sum < 0.000001 ? 0.f : (fl_mag - bl_mag) / magl_sum;
  1035. float mag_difr = magr_sum < 0.000001 ? 0.f : (fr_mag - br_mag) / magr_sum;
  1036. float mag_totall = hypotf(fl_mag, bl_mag);
  1037. float mag_totalr = hypotf(fr_mag, br_mag);
  1038. float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
  1039. float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
  1040. float xl, yl;
  1041. float xr, yr;
  1042. if (phase_difl > M_PI)
  1043. phase_difl = 2 * M_PI - phase_difl;
  1044. if (phase_difr > M_PI)
  1045. phase_difr = 2 * M_PI - phase_difr;
  1046. stereo_position(mag_difl, phase_difl, &xl, &yl);
  1047. stereo_position(mag_difr, phase_difr, &xr, &yr);
  1048. s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
  1049. mag_totall, mag_totalr,
  1050. fl_phase, fr_phase,
  1051. bl_phase, br_phase,
  1052. sl_phase, sr_phase,
  1053. xl, yl, xr, yr, n);
  1054. }
  1055. }
  1056. static int init(AVFilterContext *ctx)
  1057. {
  1058. AudioSurroundContext *s = ctx->priv;
  1059. float overlap;
  1060. int i;
  1061. if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
  1062. av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
  1063. s->out_channel_layout_str);
  1064. return AVERROR(EINVAL);
  1065. }
  1066. if (!(s->in_channel_layout = av_get_channel_layout(s->in_channel_layout_str))) {
  1067. av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
  1068. s->in_channel_layout_str);
  1069. return AVERROR(EINVAL);
  1070. }
  1071. if (s->lowcutf >= s->highcutf) {
  1072. av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
  1073. s->lowcutf, s->highcutf);
  1074. return AVERROR(EINVAL);
  1075. }
  1076. switch (s->in_channel_layout) {
  1077. case AV_CH_LAYOUT_STEREO:
  1078. s->filter = filter_stereo;
  1079. switch (s->out_channel_layout) {
  1080. case AV_CH_LAYOUT_MONO:
  1081. s->upmix_stereo = upmix_1_0;
  1082. break;
  1083. case AV_CH_LAYOUT_STEREO:
  1084. s->upmix_stereo = upmix_stereo;
  1085. break;
  1086. case AV_CH_LAYOUT_2POINT1:
  1087. s->upmix_stereo = upmix_2_1;
  1088. break;
  1089. case AV_CH_LAYOUT_SURROUND:
  1090. s->upmix_stereo = upmix_3_0;
  1091. break;
  1092. case AV_CH_LAYOUT_3POINT1:
  1093. s->upmix_stereo = upmix_3_1;
  1094. break;
  1095. case AV_CH_LAYOUT_4POINT0:
  1096. s->upmix_stereo = upmix_4_0;
  1097. break;
  1098. case AV_CH_LAYOUT_4POINT1:
  1099. s->upmix_stereo = upmix_4_1;
  1100. break;
  1101. case AV_CH_LAYOUT_5POINT0_BACK:
  1102. s->upmix_stereo = upmix_5_0_back;
  1103. break;
  1104. case AV_CH_LAYOUT_5POINT1_BACK:
  1105. s->upmix_stereo = upmix_5_1_back;
  1106. break;
  1107. case AV_CH_LAYOUT_6POINT0:
  1108. s->upmix_stereo = upmix_6_0;
  1109. break;
  1110. case AV_CH_LAYOUT_6POINT1:
  1111. s->upmix_stereo = upmix_6_1;
  1112. break;
  1113. case AV_CH_LAYOUT_7POINT0:
  1114. s->upmix_stereo = upmix_7_0;
  1115. break;
  1116. case AV_CH_LAYOUT_7POINT1:
  1117. s->upmix_stereo = upmix_7_1;
  1118. break;
  1119. default:
  1120. goto fail;
  1121. }
  1122. break;
  1123. case AV_CH_LAYOUT_2POINT1:
  1124. s->filter = filter_2_1;
  1125. switch (s->out_channel_layout) {
  1126. case AV_CH_LAYOUT_5POINT1_BACK:
  1127. s->upmix_2_1 = upmix_5_1_back_2_1;
  1128. break;
  1129. default:
  1130. goto fail;
  1131. }
  1132. break;
  1133. case AV_CH_LAYOUT_SURROUND:
  1134. s->filter = filter_surround;
  1135. switch (s->out_channel_layout) {
  1136. case AV_CH_LAYOUT_3POINT1:
  1137. s->upmix_3_0 = upmix_3_1_surround;
  1138. break;
  1139. case AV_CH_LAYOUT_5POINT1_BACK:
  1140. s->upmix_3_0 = upmix_5_1_back_surround;
  1141. break;
  1142. default:
  1143. goto fail;
  1144. }
  1145. break;
  1146. case AV_CH_LAYOUT_5POINT0:
  1147. s->filter = filter_5_0_side;
  1148. switch (s->out_channel_layout) {
  1149. case AV_CH_LAYOUT_7POINT1:
  1150. s->upmix_5_0 = upmix_7_1_5_0_side;
  1151. break;
  1152. default:
  1153. goto fail;
  1154. }
  1155. break;
  1156. case AV_CH_LAYOUT_5POINT1:
  1157. s->filter = filter_5_1_side;
  1158. switch (s->out_channel_layout) {
  1159. case AV_CH_LAYOUT_7POINT1:
  1160. s->upmix_5_1 = upmix_7_1_5_1;
  1161. break;
  1162. default:
  1163. goto fail;
  1164. }
  1165. break;
  1166. case AV_CH_LAYOUT_5POINT1_BACK:
  1167. s->filter = filter_5_1_back;
  1168. switch (s->out_channel_layout) {
  1169. case AV_CH_LAYOUT_7POINT1:
  1170. s->upmix_5_1 = upmix_7_1_5_1;
  1171. break;
  1172. default:
  1173. goto fail;
  1174. }
  1175. break;
  1176. default:
  1177. fail:
  1178. av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
  1179. s->in_channel_layout_str, s->out_channel_layout_str);
  1180. return AVERROR(EINVAL);
  1181. }
  1182. s->buf_size = 4096;
  1183. s->pts = AV_NOPTS_VALUE;
  1184. s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
  1185. if (!s->window_func_lut)
  1186. return AVERROR(ENOMEM);
  1187. generate_window_func(s->window_func_lut, s->buf_size, s->win_func, &overlap);
  1188. if (s->overlap == 1)
  1189. s->overlap = overlap;
  1190. for (i = 0; i < s->buf_size; i++)
  1191. s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->buf_size);
  1192. s->hop_size = s->buf_size * (1. - s->overlap);
  1193. if (s->hop_size <= 0)
  1194. return AVERROR(EINVAL);
  1195. if (s->all_x >= 0.f)
  1196. s->fc_x = s->fl_x = s->fr_x = s->bc_x = s->sl_x = s->sr_x = s->bl_x = s->br_x = s->all_x;
  1197. if (s->all_y >= 0.f)
  1198. s->fc_y = s->fl_y = s->fr_y = s->bc_y = s->sl_y = s->sr_y = s->bl_y = s->br_y = s->all_y;
  1199. return 0;
  1200. }
  1201. static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  1202. {
  1203. AudioSurroundContext *s = ctx->priv;
  1204. const float level_in = s->input_levels[ch];
  1205. float *dst;
  1206. int n;
  1207. memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
  1208. dst = (float *)s->input->extended_data[ch];
  1209. for (n = 0; n < s->buf_size; n++) {
  1210. dst[n] *= s->window_func_lut[n] * level_in;
  1211. }
  1212. av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
  1213. return 0;
  1214. }
  1215. static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  1216. {
  1217. AudioSurroundContext *s = ctx->priv;
  1218. const float level_out = s->output_levels[ch];
  1219. AVFrame *out = arg;
  1220. float *dst, *ptr;
  1221. int n;
  1222. av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
  1223. dst = (float *)s->output->extended_data[ch];
  1224. ptr = (float *)s->overlap_buffer->extended_data[ch];
  1225. memmove(s->overlap_buffer->extended_data[ch],
  1226. s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
  1227. s->buf_size * sizeof(float));
  1228. memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
  1229. 0, s->hop_size * sizeof(float));
  1230. for (n = 0; n < s->buf_size; n++) {
  1231. ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
  1232. }
  1233. ptr = (float *)s->overlap_buffer->extended_data[ch];
  1234. dst = (float *)out->extended_data[ch];
  1235. memcpy(dst, ptr, s->hop_size * sizeof(float));
  1236. return 0;
  1237. }
  1238. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  1239. {
  1240. AVFilterContext *ctx = inlink->dst;
  1241. AVFilterLink *outlink = ctx->outputs[0];
  1242. AudioSurroundContext *s = ctx->priv;
  1243. int ret;
  1244. ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  1245. in->nb_samples);
  1246. if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
  1247. s->pts = in->pts;
  1248. av_frame_free(&in);
  1249. if (ret < 0)
  1250. return ret;
  1251. while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
  1252. AVFrame *out;
  1253. ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
  1254. if (ret < 0)
  1255. return ret;
  1256. ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
  1257. s->filter(ctx);
  1258. out = ff_get_audio_buffer(outlink, s->hop_size);
  1259. if (!out)
  1260. return AVERROR(ENOMEM);
  1261. ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
  1262. out->pts = s->pts;
  1263. if (s->pts != AV_NOPTS_VALUE)
  1264. s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  1265. av_audio_fifo_drain(s->fifo, s->hop_size);
  1266. ret = ff_filter_frame(outlink, out);
  1267. if (ret < 0)
  1268. return ret;
  1269. }
  1270. return 0;
  1271. }
  1272. static int request_frame(AVFilterLink *outlink)
  1273. {
  1274. AVFilterContext *ctx = outlink->src;
  1275. AudioSurroundContext *s = ctx->priv;
  1276. int ret = 0;
  1277. ret = ff_request_frame(ctx->inputs[0]);
  1278. if (ret == AVERROR_EOF && av_audio_fifo_size(s->fifo) > 0 && av_audio_fifo_size(s->fifo) < s->buf_size) {
  1279. AVFrame *in;
  1280. in = ff_get_audio_buffer(outlink, s->buf_size - av_audio_fifo_size(s->fifo));
  1281. if (!in)
  1282. return AVERROR(ENOMEM);
  1283. ret = filter_frame(ctx->inputs[0], in);
  1284. av_audio_fifo_drain(s->fifo, s->buf_size);
  1285. }
  1286. return ret;
  1287. }
  1288. static av_cold void uninit(AVFilterContext *ctx)
  1289. {
  1290. AudioSurroundContext *s = ctx->priv;
  1291. int ch;
  1292. av_frame_free(&s->input);
  1293. av_frame_free(&s->output);
  1294. av_frame_free(&s->overlap_buffer);
  1295. for (ch = 0; ch < s->nb_in_channels; ch++) {
  1296. av_rdft_end(s->rdft[ch]);
  1297. }
  1298. for (ch = 0; ch < s->nb_out_channels; ch++) {
  1299. av_rdft_end(s->irdft[ch]);
  1300. }
  1301. av_freep(&s->input_levels);
  1302. av_freep(&s->output_levels);
  1303. av_freep(&s->rdft);
  1304. av_freep(&s->irdft);
  1305. av_audio_fifo_free(s->fifo);
  1306. av_freep(&s->window_func_lut);
  1307. }
  1308. #define OFFSET(x) offsetof(AudioSurroundContext, x)
  1309. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  1310. static const AVOption surround_options[] = {
  1311. { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
  1312. { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
  1313. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  1314. { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  1315. { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  1316. { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
  1317. { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
  1318. { "lfe_mode", "set LFE channel mode", OFFSET(lfe_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "lfe_mode" },
  1319. { "add", "just add LFE channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 1, FLAGS, "lfe_mode" },
  1320. { "sub", "substract LFE channel with others", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 1, FLAGS, "lfe_mode" },
  1321. { "fc_in", "set front center channel input level", OFFSET(fc_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  1322. { "fc_out", "set front center channel output level", OFFSET(fc_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  1323. { "lfe_in", "set lfe channel input level", OFFSET(lfe_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  1324. { "lfe_out", "set lfe channel output level", OFFSET(lfe_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  1325. { "allx", "set all channel's x spread", OFFSET(all_x), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 15, FLAGS },
  1326. { "ally", "set all channel's y spread", OFFSET(all_y), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 15, FLAGS },
  1327. { "fcx", "set front center channel x spread", OFFSET(fc_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1328. { "flx", "set front left channel x spread", OFFSET(fl_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1329. { "frx", "set front right channel x spread", OFFSET(fr_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1330. { "blx", "set back left channel x spread", OFFSET(bl_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1331. { "brx", "set back right channel x spread", OFFSET(br_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1332. { "slx", "set side left channel x spread", OFFSET(sl_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1333. { "srx", "set side right channel x spread", OFFSET(sr_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1334. { "bcx", "set back center channel x spread", OFFSET(bc_x), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1335. { "fcy", "set front center channel y spread", OFFSET(fc_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1336. { "fly", "set front left channel y spread", OFFSET(fl_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1337. { "fry", "set front right channel y spread", OFFSET(fr_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1338. { "bly", "set back left channel y spread", OFFSET(bl_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1339. { "bry", "set back right channel y spread", OFFSET(br_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1340. { "sly", "set side left channel y spread", OFFSET(sl_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1341. { "sry", "set side right channel y spread", OFFSET(sr_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1342. { "bcy", "set back center channel y spread", OFFSET(bc_y), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 15, FLAGS },
  1343. { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
  1344. { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
  1345. { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
  1346. { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  1347. { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
  1348. { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
  1349. { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
  1350. { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
  1351. { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
  1352. { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
  1353. { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
  1354. { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
  1355. { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
  1356. { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
  1357. { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
  1358. { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
  1359. { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
  1360. { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
  1361. { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
  1362. { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
  1363. { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
  1364. { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
  1365. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
  1366. { NULL }
  1367. };
  1368. AVFILTER_DEFINE_CLASS(surround);
  1369. static const AVFilterPad inputs[] = {
  1370. {
  1371. .name = "default",
  1372. .type = AVMEDIA_TYPE_AUDIO,
  1373. .filter_frame = filter_frame,
  1374. .config_props = config_input,
  1375. },
  1376. { NULL }
  1377. };
  1378. static const AVFilterPad outputs[] = {
  1379. {
  1380. .name = "default",
  1381. .type = AVMEDIA_TYPE_AUDIO,
  1382. .request_frame = request_frame,
  1383. .config_props = config_output,
  1384. },
  1385. { NULL }
  1386. };
  1387. AVFilter ff_af_surround = {
  1388. .name = "surround",
  1389. .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
  1390. .query_formats = query_formats,
  1391. .priv_size = sizeof(AudioSurroundContext),
  1392. .priv_class = &surround_class,
  1393. .init = init,
  1394. .uninit = uninit,
  1395. .inputs = inputs,
  1396. .outputs = outputs,
  1397. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1398. };