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.

1265 lines
43KB

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