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.

1489 lines
51KB

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