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.

836 lines
27KB

  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. float level_in;
  31. float level_out;
  32. int output_lfe;
  33. int lowcutf;
  34. int highcutf;
  35. float lowcut;
  36. float highcut;
  37. uint64_t out_channel_layout;
  38. int nb_in_channels;
  39. int nb_out_channels;
  40. AVFrame *input;
  41. AVFrame *output;
  42. AVFrame *overlap_buffer;
  43. int buf_size;
  44. int hop_size;
  45. AVAudioFifo *fifo;
  46. RDFTContext **rdft, **irdft;
  47. float *window_func_lut;
  48. int64_t pts;
  49. void (*upmix)(AVFilterContext *ctx,
  50. float l_phase,
  51. float r_phase,
  52. float c_phase,
  53. float mag_total,
  54. float x, float y,
  55. int n);
  56. } AudioSurroundContext;
  57. static int query_formats(AVFilterContext *ctx)
  58. {
  59. AudioSurroundContext *s = ctx->priv;
  60. AVFilterFormats *formats = NULL;
  61. AVFilterChannelLayouts *layouts = NULL;
  62. int ret;
  63. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
  64. if (ret)
  65. return ret;
  66. ret = ff_set_common_formats(ctx, formats);
  67. if (ret)
  68. return ret;
  69. layouts = NULL;
  70. ret = ff_add_channel_layout(&layouts, s->out_channel_layout);
  71. if (ret)
  72. return ret;
  73. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts);
  74. if (ret)
  75. return ret;
  76. layouts = NULL;
  77. ret = ff_add_channel_layout(&layouts, AV_CH_LAYOUT_STEREO);
  78. if (ret)
  79. return ret;
  80. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->out_channel_layouts);
  81. if (ret)
  82. return ret;
  83. formats = ff_all_samplerates();
  84. if (!formats)
  85. return AVERROR(ENOMEM);
  86. return ff_set_common_samplerates(ctx, formats);
  87. }
  88. static int config_input(AVFilterLink *inlink)
  89. {
  90. AVFilterContext *ctx = inlink->dst;
  91. AudioSurroundContext *s = ctx->priv;
  92. int ch;
  93. s->rdft = av_calloc(inlink->channels, sizeof(*s->rdft));
  94. if (!s->rdft)
  95. return AVERROR(ENOMEM);
  96. for (ch = 0; ch < inlink->channels; ch++) {
  97. s->rdft[ch] = av_rdft_init(ff_log2(s->buf_size), DFT_R2C);
  98. if (!s->rdft[ch])
  99. return AVERROR(ENOMEM);
  100. }
  101. s->nb_in_channels = inlink->channels;
  102. s->input = ff_get_audio_buffer(inlink, s->buf_size * 2);
  103. if (!s->input)
  104. return AVERROR(ENOMEM);
  105. s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->buf_size);
  106. if (!s->fifo)
  107. return AVERROR(ENOMEM);
  108. s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
  109. s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->buf_size / 2);
  110. return 0;
  111. }
  112. static int config_output(AVFilterLink *outlink)
  113. {
  114. AVFilterContext *ctx = outlink->src;
  115. AudioSurroundContext *s = ctx->priv;
  116. int ch;
  117. s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
  118. if (!s->irdft)
  119. return AVERROR(ENOMEM);
  120. for (ch = 0; ch < outlink->channels; ch++) {
  121. s->irdft[ch] = av_rdft_init(ff_log2(s->buf_size), IDFT_C2R);
  122. if (!s->irdft[ch])
  123. return AVERROR(ENOMEM);
  124. }
  125. s->nb_out_channels = outlink->channels;
  126. s->output = ff_get_audio_buffer(outlink, s->buf_size * 2);
  127. s->overlap_buffer = ff_get_audio_buffer(outlink, s->buf_size * 2);
  128. if (!s->overlap_buffer || !s->output)
  129. return AVERROR(ENOMEM);
  130. return 0;
  131. }
  132. static void stereo_position(float a, float p, float *x, float *y)
  133. {
  134. *x = av_clipf(a+FFMAX(0, sinf(p-M_PI_2))*FFDIFFSIGN(a,0), -1, 1);
  135. *y = av_clipf(cosf(a*M_PI_2+M_PI)*cosf(M_PI_2-p/M_PI)*M_LN10+1, -1, 1);
  136. }
  137. static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
  138. float *lfe_mag, float *mag_total)
  139. {
  140. if (output_lfe && n < highcut) {
  141. *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PI*(lowcut-n)/(lowcut-highcut)));
  142. *lfe_mag *= *mag_total;
  143. *mag_total -= *lfe_mag;
  144. } else {
  145. *lfe_mag = 0.f;
  146. }
  147. }
  148. static void upmix_1_0(AVFilterContext *ctx,
  149. float l_phase,
  150. float r_phase,
  151. float c_phase,
  152. float mag_total,
  153. float x, float y,
  154. int n)
  155. {
  156. AudioSurroundContext *s = ctx->priv;
  157. float mag, *dst;
  158. dst = (float *)s->output->extended_data[0];
  159. mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  160. dst[2 * n ] = mag * cosf(c_phase);
  161. dst[2 * n + 1] = mag * sinf(c_phase);
  162. }
  163. static void upmix_stereo(AVFilterContext *ctx,
  164. float l_phase,
  165. float r_phase,
  166. float c_phase,
  167. float mag_total,
  168. float x, float y,
  169. int n)
  170. {
  171. AudioSurroundContext *s = ctx->priv;
  172. float l_mag, r_mag, *dstl, *dstr;
  173. dstl = (float *)s->output->extended_data[0];
  174. dstr = (float *)s->output->extended_data[1];
  175. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  176. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  177. dstl[2 * n ] = l_mag * cosf(l_phase);
  178. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  179. dstr[2 * n ] = r_mag * cosf(r_phase);
  180. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  181. }
  182. static void upmix_2_1(AVFilterContext *ctx,
  183. float l_phase,
  184. float r_phase,
  185. float c_phase,
  186. float mag_total,
  187. float x, float y,
  188. int n)
  189. {
  190. AudioSurroundContext *s = ctx->priv;
  191. float lfe_mag, l_mag, r_mag, *dstl, *dstr, *dstlfe;
  192. dstl = (float *)s->output->extended_data[0];
  193. dstr = (float *)s->output->extended_data[1];
  194. dstlfe = (float *)s->output->extended_data[2];
  195. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  196. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  197. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  198. dstl[2 * n ] = l_mag * cosf(l_phase);
  199. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  200. dstr[2 * n ] = r_mag * cosf(r_phase);
  201. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  202. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  203. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  204. }
  205. static void upmix_3_0(AVFilterContext *ctx,
  206. float l_phase,
  207. float r_phase,
  208. float c_phase,
  209. float mag_total,
  210. float x, float y,
  211. int n)
  212. {
  213. AudioSurroundContext *s = ctx->priv;
  214. float l_mag, r_mag, c_mag, *dstc, *dstl, *dstr;
  215. dstl = (float *)s->output->extended_data[0];
  216. dstr = (float *)s->output->extended_data[1];
  217. dstc = (float *)s->output->extended_data[2];
  218. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  219. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  220. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  221. dstl[2 * n ] = l_mag * cosf(l_phase);
  222. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  223. dstr[2 * n ] = r_mag * cosf(r_phase);
  224. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  225. dstc[2 * n ] = c_mag * cosf(c_phase);
  226. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  227. }
  228. static void upmix_3_1(AVFilterContext *ctx,
  229. float l_phase,
  230. float r_phase,
  231. float c_phase,
  232. float mag_total,
  233. float x, float y,
  234. int n)
  235. {
  236. AudioSurroundContext *s = ctx->priv;
  237. float lfe_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstlfe;
  238. dstl = (float *)s->output->extended_data[0];
  239. dstr = (float *)s->output->extended_data[1];
  240. dstc = (float *)s->output->extended_data[2];
  241. dstlfe = (float *)s->output->extended_data[3];
  242. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  243. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  244. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  245. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  246. dstl[2 * n ] = l_mag * cosf(l_phase);
  247. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  248. dstr[2 * n ] = r_mag * cosf(r_phase);
  249. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  250. dstc[2 * n ] = c_mag * cosf(c_phase);
  251. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  252. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  253. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  254. }
  255. static void upmix_4_0(AVFilterContext *ctx,
  256. float l_phase,
  257. float r_phase,
  258. float c_phase,
  259. float mag_total,
  260. float x, float y,
  261. int n)
  262. {
  263. float b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb;
  264. AudioSurroundContext *s = ctx->priv;
  265. dstl = (float *)s->output->extended_data[0];
  266. dstr = (float *)s->output->extended_data[1];
  267. dstc = (float *)s->output->extended_data[2];
  268. dstb = (float *)s->output->extended_data[3];
  269. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  270. b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .5f) * mag_total;
  271. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  272. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  273. dstl[2 * n ] = l_mag * cosf(l_phase);
  274. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  275. dstr[2 * n ] = r_mag * cosf(r_phase);
  276. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  277. dstc[2 * n ] = c_mag * cosf(c_phase);
  278. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  279. dstb[2 * n ] = b_mag * cosf(c_phase);
  280. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  281. }
  282. static void upmix_4_1(AVFilterContext *ctx,
  283. float l_phase,
  284. float r_phase,
  285. float c_phase,
  286. float mag_total,
  287. float x, float y,
  288. int n)
  289. {
  290. float lfe_mag, b_mag, l_mag, r_mag, c_mag, *dstc, *dstl, *dstr, *dstb, *dstlfe;
  291. AudioSurroundContext *s = ctx->priv;
  292. dstl = (float *)s->output->extended_data[0];
  293. dstr = (float *)s->output->extended_data[1];
  294. dstc = (float *)s->output->extended_data[2];
  295. dstlfe = (float *)s->output->extended_data[3];
  296. dstb = (float *)s->output->extended_data[4];
  297. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  298. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  299. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  300. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  301. b_mag = sqrtf(1.f - fabsf(x)) * ((1.f - y) * .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. dstb[2 * n ] = b_mag * cosf(c_phase);
  311. dstb[2 * n + 1] = b_mag * sinf(c_phase);
  312. }
  313. static void upmix_5_0_back(AVFilterContext *ctx,
  314. float l_phase,
  315. float r_phase,
  316. float c_phase,
  317. float mag_total,
  318. float x, float y,
  319. int n)
  320. {
  321. float l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs;
  322. AudioSurroundContext *s = ctx->priv;
  323. dstl = (float *)s->output->extended_data[0];
  324. dstr = (float *)s->output->extended_data[1];
  325. dstc = (float *)s->output->extended_data[2];
  326. dstls = (float *)s->output->extended_data[3];
  327. dstrs = (float *)s->output->extended_data[4];
  328. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  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. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  332. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  333. dstl[2 * n ] = l_mag * cosf(l_phase);
  334. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  335. dstr[2 * n ] = r_mag * cosf(r_phase);
  336. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  337. dstc[2 * n ] = c_mag * cosf(c_phase);
  338. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  339. dstls[2 * n ] = ls_mag * cosf(l_phase);
  340. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  341. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  342. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  343. }
  344. static void upmix_5_1_back(AVFilterContext *ctx,
  345. float l_phase,
  346. float r_phase,
  347. float c_phase,
  348. float mag_total,
  349. float x, float y,
  350. int n)
  351. {
  352. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlfe;
  353. AudioSurroundContext *s = ctx->priv;
  354. dstl = (float *)s->output->extended_data[0];
  355. dstr = (float *)s->output->extended_data[1];
  356. dstc = (float *)s->output->extended_data[2];
  357. dstlfe = (float *)s->output->extended_data[3];
  358. dstls = (float *)s->output->extended_data[4];
  359. dstrs = (float *)s->output->extended_data[5];
  360. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  361. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  362. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  363. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  364. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  365. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  366. dstl[2 * n ] = l_mag * cosf(l_phase);
  367. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  368. dstr[2 * n ] = r_mag * cosf(r_phase);
  369. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  370. dstc[2 * n ] = c_mag * cosf(c_phase);
  371. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  372. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  373. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  374. dstls[2 * n ] = ls_mag * cosf(l_phase);
  375. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  376. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  377. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  378. }
  379. static void upmix_7_0(AVFilterContext *ctx,
  380. float l_phase,
  381. float r_phase,
  382. float c_phase,
  383. float mag_total,
  384. float x, float y,
  385. int n)
  386. {
  387. float l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
  388. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb;
  389. AudioSurroundContext *s = ctx->priv;
  390. dstl = (float *)s->output->extended_data[0];
  391. dstr = (float *)s->output->extended_data[1];
  392. dstc = (float *)s->output->extended_data[2];
  393. dstlb = (float *)s->output->extended_data[3];
  394. dstrb = (float *)s->output->extended_data[4];
  395. dstls = (float *)s->output->extended_data[5];
  396. dstrs = (float *)s->output->extended_data[6];
  397. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  398. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  399. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  400. lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  401. rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  402. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  403. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  404. dstl[2 * n ] = l_mag * cosf(l_phase);
  405. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  406. dstr[2 * n ] = r_mag * cosf(r_phase);
  407. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  408. dstc[2 * n ] = c_mag * cosf(c_phase);
  409. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  410. dstlb[2 * n ] = lb_mag * cosf(l_phase);
  411. dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
  412. dstrb[2 * n ] = rb_mag * cosf(r_phase);
  413. dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
  414. dstls[2 * n ] = ls_mag * cosf(l_phase);
  415. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  416. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  417. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  418. }
  419. static void upmix_7_1(AVFilterContext *ctx,
  420. float l_phase,
  421. float r_phase,
  422. float c_phase,
  423. float mag_total,
  424. float x, float y,
  425. int n)
  426. {
  427. float lfe_mag, l_mag, r_mag, ls_mag, rs_mag, c_mag, lb_mag, rb_mag;
  428. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  429. AudioSurroundContext *s = ctx->priv;
  430. dstl = (float *)s->output->extended_data[0];
  431. dstr = (float *)s->output->extended_data[1];
  432. dstc = (float *)s->output->extended_data[2];
  433. dstlfe = (float *)s->output->extended_data[3];
  434. dstlb = (float *)s->output->extended_data[4];
  435. dstrb = (float *)s->output->extended_data[5];
  436. dstls = (float *)s->output->extended_data[6];
  437. dstrs = (float *)s->output->extended_data[7];
  438. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, &mag_total);
  439. c_mag = sqrtf(1.f - fabsf(x)) * ((y + 1.f) * .5f) * mag_total;
  440. l_mag = sqrtf(.5f * ( x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  441. r_mag = sqrtf(.5f * (-x + 1.f)) * ((y + 1.f) * .5f) * mag_total;
  442. lb_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  443. rb_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - ((y + 1.f) * .5f)) * mag_total;
  444. ls_mag = sqrtf(.5f * ( x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  445. rs_mag = sqrtf(.5f * (-x + 1.f)) * (1.f - fabsf(y)) * mag_total;
  446. dstl[2 * n ] = l_mag * cosf(l_phase);
  447. dstl[2 * n + 1] = l_mag * sinf(l_phase);
  448. dstr[2 * n ] = r_mag * cosf(r_phase);
  449. dstr[2 * n + 1] = r_mag * sinf(r_phase);
  450. dstc[2 * n ] = c_mag * cosf(c_phase);
  451. dstc[2 * n + 1] = c_mag * sinf(c_phase);
  452. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  453. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  454. dstlb[2 * n ] = lb_mag * cosf(l_phase);
  455. dstlb[2 * n + 1] = lb_mag * sinf(l_phase);
  456. dstrb[2 * n ] = rb_mag * cosf(r_phase);
  457. dstrb[2 * n + 1] = rb_mag * sinf(r_phase);
  458. dstls[2 * n ] = ls_mag * cosf(l_phase);
  459. dstls[2 * n + 1] = ls_mag * sinf(l_phase);
  460. dstrs[2 * n ] = rs_mag * cosf(r_phase);
  461. dstrs[2 * n + 1] = rs_mag * sinf(r_phase);
  462. }
  463. static int init(AVFilterContext *ctx)
  464. {
  465. AudioSurroundContext *s = ctx->priv;
  466. float overlap;
  467. int i;
  468. if (!(s->out_channel_layout = av_get_channel_layout(s->out_channel_layout_str))) {
  469. av_log(ctx, AV_LOG_ERROR, "Error parsing channel layout '%s'.\n",
  470. s->out_channel_layout_str);
  471. return AVERROR(EINVAL);
  472. }
  473. if (s->lowcutf >= s->highcutf) {
  474. av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
  475. s->lowcutf, s->highcutf);
  476. return AVERROR(EINVAL);
  477. }
  478. switch (s->out_channel_layout) {
  479. case AV_CH_LAYOUT_MONO:
  480. s->upmix = upmix_1_0;
  481. break;
  482. case AV_CH_LAYOUT_STEREO:
  483. s->upmix = upmix_stereo;
  484. break;
  485. case AV_CH_LAYOUT_2POINT1:
  486. s->upmix = upmix_2_1;
  487. break;
  488. case AV_CH_LAYOUT_SURROUND:
  489. s->upmix = upmix_3_0;
  490. break;
  491. case AV_CH_LAYOUT_3POINT1:
  492. s->upmix = upmix_3_1;
  493. break;
  494. case AV_CH_LAYOUT_4POINT0:
  495. s->upmix = upmix_4_0;
  496. break;
  497. case AV_CH_LAYOUT_4POINT1:
  498. s->upmix = upmix_4_1;
  499. break;
  500. case AV_CH_LAYOUT_5POINT0_BACK:
  501. s->upmix = upmix_5_0_back;
  502. break;
  503. case AV_CH_LAYOUT_5POINT1_BACK:
  504. s->upmix = upmix_5_1_back;
  505. break;
  506. case AV_CH_LAYOUT_7POINT0:
  507. s->upmix = upmix_7_0;
  508. break;
  509. case AV_CH_LAYOUT_7POINT1:
  510. s->upmix = upmix_7_1;
  511. break;
  512. default:
  513. av_log(ctx, AV_LOG_ERROR, "Unsupported output channel layout '%s'.\n",
  514. s->out_channel_layout_str);
  515. return AVERROR(EINVAL);
  516. }
  517. s->buf_size = 4096;
  518. s->pts = AV_NOPTS_VALUE;
  519. s->window_func_lut = av_calloc(s->buf_size, sizeof(*s->window_func_lut));
  520. if (!s->window_func_lut)
  521. return AVERROR(ENOMEM);
  522. for (i = 0; i < s->buf_size; i++)
  523. s->window_func_lut[i] = sqrtf(0.5 * (1 - cosf(2 * M_PI * i / s->buf_size)) / s->buf_size);
  524. overlap = .5;
  525. s->hop_size = s->buf_size * (1. - overlap);
  526. return 0;
  527. }
  528. static int fft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  529. {
  530. AudioSurroundContext *s = ctx->priv;
  531. const float level_in = s->level_in;
  532. float *dst;
  533. int n;
  534. memset(s->input->extended_data[ch] + s->buf_size * sizeof(float), 0, s->buf_size * sizeof(float));
  535. dst = (float *)s->input->extended_data[ch];
  536. for (n = 0; n < s->buf_size; n++) {
  537. dst[n] *= s->window_func_lut[n] * level_in;
  538. }
  539. av_rdft_calc(s->rdft[ch], (float *)s->input->extended_data[ch]);
  540. return 0;
  541. }
  542. static int ifft_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  543. {
  544. AudioSurroundContext *s = ctx->priv;
  545. const float level_out = s->level_out;
  546. AVFrame *out = arg;
  547. float *dst, *ptr;
  548. int n;
  549. av_rdft_calc(s->irdft[ch], (float *)s->output->extended_data[ch]);
  550. dst = (float *)s->output->extended_data[ch];
  551. ptr = (float *)s->overlap_buffer->extended_data[ch];
  552. memmove(s->overlap_buffer->extended_data[ch],
  553. s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
  554. s->buf_size * sizeof(float));
  555. memset(s->overlap_buffer->extended_data[ch] + s->buf_size * sizeof(float),
  556. 0, s->hop_size * sizeof(float));
  557. for (n = 0; n < s->buf_size; n++) {
  558. ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
  559. }
  560. ptr = (float *)s->overlap_buffer->extended_data[ch];
  561. dst = (float *)out->extended_data[ch];
  562. memcpy(dst, ptr, s->hop_size * sizeof(float));
  563. return 0;
  564. }
  565. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  566. {
  567. AVFilterContext *ctx = inlink->dst;
  568. AVFilterLink *outlink = ctx->outputs[0];
  569. AudioSurroundContext *s = ctx->priv;
  570. av_audio_fifo_write(s->fifo, (void **)in->extended_data,
  571. in->nb_samples);
  572. if (s->pts == AV_NOPTS_VALUE)
  573. s->pts = in->pts;
  574. av_frame_free(&in);
  575. while (av_audio_fifo_size(s->fifo) >= s->buf_size) {
  576. float *srcl, *srcr;
  577. AVFrame *out;
  578. int n, ret;
  579. ret = av_audio_fifo_peek(s->fifo, (void **)s->input->extended_data, s->buf_size);
  580. if (ret < 0)
  581. return ret;
  582. ctx->internal->execute(ctx, fft_channel, NULL, NULL, inlink->channels);
  583. srcl = (float *)s->input->extended_data[0];
  584. srcr = (float *)s->input->extended_data[1];
  585. for (n = 0; n < s->buf_size; n++) {
  586. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  587. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  588. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  589. float l_mag = hypotf(l_re, l_im);
  590. float r_mag = hypotf(r_re, r_im);
  591. float l_phase = atan2f(l_im, l_re);
  592. float r_phase = atan2f(r_im, r_re);
  593. float phase_dif = fabsf(l_phase - r_phase);
  594. float mag_dif = (l_mag - r_mag) / (l_mag + r_mag);
  595. float mag_total = hypotf(l_mag, r_mag);
  596. float x, y;
  597. if (phase_dif > M_PI)
  598. phase_dif = 2 * M_PI - phase_dif;
  599. stereo_position(mag_dif, phase_dif, &x, &y);
  600. s->upmix(ctx, l_phase, r_phase, c_phase, mag_total, x, y, n);
  601. }
  602. out = ff_get_audio_buffer(outlink, s->hop_size);
  603. if (!out)
  604. return AVERROR(ENOMEM);
  605. ctx->internal->execute(ctx, ifft_channel, out, NULL, outlink->channels);
  606. out->pts = s->pts;
  607. if (s->pts != AV_NOPTS_VALUE)
  608. s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  609. av_audio_fifo_drain(s->fifo, s->hop_size);
  610. ret = ff_filter_frame(outlink, out);
  611. if (ret < 0)
  612. return ret;
  613. }
  614. return 0;
  615. }
  616. static av_cold void uninit(AVFilterContext *ctx)
  617. {
  618. AudioSurroundContext *s = ctx->priv;
  619. int ch;
  620. av_frame_free(&s->input);
  621. av_frame_free(&s->output);
  622. av_frame_free(&s->overlap_buffer);
  623. for (ch = 0; ch < s->nb_in_channels; ch++) {
  624. av_rdft_end(s->rdft[ch]);
  625. }
  626. for (ch = 0; ch < s->nb_out_channels; ch++) {
  627. av_rdft_end(s->irdft[ch]);
  628. }
  629. av_freep(&s->rdft);
  630. av_freep(&s->irdft);
  631. av_audio_fifo_free(s->fifo);
  632. av_freep(&s->window_func_lut);
  633. }
  634. #define OFFSET(x) offsetof(AudioSurroundContext, x)
  635. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  636. static const AVOption surround_options[] = {
  637. { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
  638. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  639. { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, FLAGS },
  640. { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  641. { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
  642. { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
  643. { NULL }
  644. };
  645. AVFILTER_DEFINE_CLASS(surround);
  646. static const AVFilterPad inputs[] = {
  647. {
  648. .name = "default",
  649. .type = AVMEDIA_TYPE_AUDIO,
  650. .filter_frame = filter_frame,
  651. .config_props = config_input,
  652. },
  653. { NULL }
  654. };
  655. static const AVFilterPad outputs[] = {
  656. {
  657. .name = "default",
  658. .type = AVMEDIA_TYPE_AUDIO,
  659. .config_props = config_output,
  660. },
  661. { NULL }
  662. };
  663. AVFilter ff_af_surround = {
  664. .name = "surround",
  665. .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
  666. .query_formats = query_formats,
  667. .priv_size = sizeof(AudioSurroundContext),
  668. .priv_class = &surround_class,
  669. .init = init,
  670. .uninit = uninit,
  671. .inputs = inputs,
  672. .outputs = outputs,
  673. .flags = AVFILTER_FLAG_SLICE_THREADS,
  674. };