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.

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