af_surround.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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/avassert.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/opt.h"
  23. #include "libavutil/tx.h"
  24. #include "avfilter.h"
  25. #include "audio.h"
  26. #include "filters.h"
  27. #include "internal.h"
  28. #include "formats.h"
  29. #include "window_func.h"
  30. enum SurroundChannel {
  31. SC_FL, SC_FR, SC_FC, SC_LF, SC_BL, SC_BR, SC_BC, SC_SL, SC_SR,
  32. SC_NB,
  33. };
  34. static const int ch_map[SC_NB] = {
  35. [SC_FL] = AV_CHAN_FRONT_LEFT,
  36. [SC_FR] = AV_CHAN_FRONT_RIGHT,
  37. [SC_FC] = AV_CHAN_FRONT_CENTER,
  38. [SC_LF] = AV_CHAN_LOW_FREQUENCY,
  39. [SC_BL] = AV_CHAN_BACK_LEFT,
  40. [SC_BR] = AV_CHAN_BACK_RIGHT,
  41. [SC_BC] = AV_CHAN_BACK_CENTER,
  42. [SC_SL] = AV_CHAN_SIDE_LEFT,
  43. [SC_SR] = AV_CHAN_SIDE_RIGHT,
  44. };
  45. static const int sc_map[16] = {
  46. [AV_CHAN_FRONT_LEFT ] = SC_FL,
  47. [AV_CHAN_FRONT_RIGHT ] = SC_FR,
  48. [AV_CHAN_FRONT_CENTER ] = SC_FC,
  49. [AV_CHAN_LOW_FREQUENCY] = SC_LF,
  50. [AV_CHAN_BACK_LEFT ] = SC_BL,
  51. [AV_CHAN_BACK_RIGHT ] = SC_BR,
  52. [AV_CHAN_BACK_CENTER ] = SC_BC,
  53. [AV_CHAN_SIDE_LEFT ] = SC_SL,
  54. [AV_CHAN_SIDE_RIGHT ] = SC_SR,
  55. };
  56. typedef struct AudioSurroundContext {
  57. const AVClass *class;
  58. char *out_channel_layout_str;
  59. char *in_channel_layout_str;
  60. float level_in;
  61. float level_out;
  62. float f_i[SC_NB];
  63. float f_o[SC_NB];
  64. int lfe_mode;
  65. float smooth;
  66. float angle;
  67. float focus;
  68. int win_size;
  69. int win_func;
  70. float win_gain;
  71. float overlap;
  72. float all_x;
  73. float all_y;
  74. float f_x[SC_NB];
  75. float f_y[SC_NB];
  76. float *input_levels;
  77. float *output_levels;
  78. int output_lfe;
  79. int create_lfe;
  80. int lowcutf;
  81. int highcutf;
  82. float lowcut;
  83. float highcut;
  84. AVChannelLayout out_ch_layout;
  85. AVChannelLayout in_ch_layout;
  86. int nb_in_channels;
  87. int nb_out_channels;
  88. AVFrame *factors;
  89. AVFrame *sfactors;
  90. AVFrame *input_in;
  91. AVFrame *input;
  92. AVFrame *output;
  93. AVFrame *output_mag;
  94. AVFrame *output_ph;
  95. AVFrame *output_out;
  96. AVFrame *overlap_buffer;
  97. AVFrame *window;
  98. float *x_pos;
  99. float *y_pos;
  100. float *l_phase;
  101. float *r_phase;
  102. float *c_phase;
  103. float *c_mag;
  104. float *lfe_mag;
  105. float *lfe_phase;
  106. float *mag_total;
  107. int rdft_size;
  108. int hop_size;
  109. AVTXContext **rdft, **irdft;
  110. av_tx_fn tx_fn, itx_fn;
  111. float *window_func_lut;
  112. void (*filter)(AVFilterContext *ctx);
  113. void (*upmix)(AVFilterContext *ctx, int ch);
  114. void (*upmix_5_0)(AVFilterContext *ctx,
  115. float c_re, float c_im,
  116. float mag_totall, float mag_totalr,
  117. float fl_phase, float fr_phase,
  118. float bl_phase, float br_phase,
  119. float sl_phase, float sr_phase,
  120. float xl, float yl,
  121. float xr, float yr,
  122. int n);
  123. void (*upmix_5_1)(AVFilterContext *ctx,
  124. float c_re, float c_im,
  125. float lfe_re, float lfe_im,
  126. float mag_totall, float mag_totalr,
  127. float fl_phase, float fr_phase,
  128. float bl_phase, float br_phase,
  129. float sl_phase, float sr_phase,
  130. float xl, float yl,
  131. float xr, float yr,
  132. int n);
  133. } AudioSurroundContext;
  134. static int query_formats(AVFilterContext *ctx)
  135. {
  136. AudioSurroundContext *s = ctx->priv;
  137. AVFilterFormats *formats = NULL;
  138. AVFilterChannelLayouts *layouts = NULL;
  139. int ret;
  140. ret = ff_add_format(&formats, AV_SAMPLE_FMT_FLTP);
  141. if (ret)
  142. return ret;
  143. ret = ff_set_common_formats(ctx, formats);
  144. if (ret)
  145. return ret;
  146. layouts = NULL;
  147. ret = ff_add_channel_layout(&layouts, &s->out_ch_layout);
  148. if (ret)
  149. return ret;
  150. ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->incfg.channel_layouts);
  151. if (ret)
  152. return ret;
  153. layouts = NULL;
  154. ret = ff_add_channel_layout(&layouts, &s->in_ch_layout);
  155. if (ret)
  156. return ret;
  157. ret = ff_channel_layouts_ref(layouts, &ctx->inputs[0]->outcfg.channel_layouts);
  158. if (ret)
  159. return ret;
  160. return ff_set_common_all_samplerates(ctx);
  161. }
  162. static void set_input_levels(AVFilterContext *ctx)
  163. {
  164. AudioSurroundContext *s = ctx->priv;
  165. for (int ch = 0; ch < s->nb_in_channels && s->level_in >= 0.f; ch++)
  166. s->input_levels[ch] = s->level_in;
  167. s->level_in = -1.f;
  168. for (int n = 0; n < SC_NB; n++) {
  169. const int ch = av_channel_layout_index_from_channel(&s->in_ch_layout, ch_map[n]);
  170. if (ch >= 0)
  171. s->input_levels[ch] = s->f_i[n];
  172. }
  173. }
  174. static void set_output_levels(AVFilterContext *ctx)
  175. {
  176. AudioSurroundContext *s = ctx->priv;
  177. for (int ch = 0; ch < s->nb_out_channels && s->level_out >= 0.f; ch++)
  178. s->output_levels[ch] = s->level_out;
  179. s->level_out = -1.f;
  180. for (int n = 0; n < SC_NB; n++) {
  181. const int ch = av_channel_layout_index_from_channel(&s->out_ch_layout, ch_map[n]);
  182. if (ch >= 0)
  183. s->output_levels[ch] = s->f_o[n];
  184. }
  185. }
  186. static int config_input(AVFilterLink *inlink)
  187. {
  188. AVFilterContext *ctx = inlink->dst;
  189. AudioSurroundContext *s = ctx->priv;
  190. int ret;
  191. s->rdft = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->rdft));
  192. if (!s->rdft)
  193. return AVERROR(ENOMEM);
  194. s->nb_in_channels = inlink->ch_layout.nb_channels;
  195. for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) {
  196. float scale = 1.f;
  197. ret = av_tx_init(&s->rdft[ch], &s->tx_fn, AV_TX_FLOAT_RDFT,
  198. 0, s->win_size, &scale, 0);
  199. if (ret < 0)
  200. return ret;
  201. }
  202. s->input_levels = av_malloc_array(s->nb_in_channels, sizeof(*s->input_levels));
  203. if (!s->input_levels)
  204. return AVERROR(ENOMEM);
  205. set_input_levels(ctx);
  206. s->window = ff_get_audio_buffer(inlink, s->win_size * 2);
  207. if (!s->window)
  208. return AVERROR(ENOMEM);
  209. s->input_in = ff_get_audio_buffer(inlink, s->win_size * 2);
  210. if (!s->input_in)
  211. return AVERROR(ENOMEM);
  212. s->input = ff_get_audio_buffer(inlink, s->win_size + 2);
  213. if (!s->input)
  214. return AVERROR(ENOMEM);
  215. s->lowcut = 1.f * s->lowcutf / (inlink->sample_rate * 0.5) * (s->win_size / 2);
  216. s->highcut = 1.f * s->highcutf / (inlink->sample_rate * 0.5) * (s->win_size / 2);
  217. return 0;
  218. }
  219. static int config_output(AVFilterLink *outlink)
  220. {
  221. AVFilterContext *ctx = outlink->src;
  222. AudioSurroundContext *s = ctx->priv;
  223. int ret;
  224. s->irdft = av_calloc(outlink->ch_layout.nb_channels, sizeof(*s->irdft));
  225. if (!s->irdft)
  226. return AVERROR(ENOMEM);
  227. s->nb_out_channels = outlink->ch_layout.nb_channels;
  228. for (int ch = 0; ch < outlink->ch_layout.nb_channels; ch++) {
  229. float iscale = 1.f;
  230. ret = av_tx_init(&s->irdft[ch], &s->itx_fn, AV_TX_FLOAT_RDFT,
  231. 1, s->win_size, &iscale, 0);
  232. if (ret < 0)
  233. return ret;
  234. }
  235. s->output_levels = av_malloc_array(s->nb_out_channels, sizeof(*s->output_levels));
  236. if (!s->output_levels)
  237. return AVERROR(ENOMEM);
  238. set_output_levels(ctx);
  239. s->factors = ff_get_audio_buffer(outlink, s->win_size + 2);
  240. s->sfactors = ff_get_audio_buffer(outlink, s->win_size + 2);
  241. s->output_ph = ff_get_audio_buffer(outlink, s->win_size + 2);
  242. s->output_mag = ff_get_audio_buffer(outlink, s->win_size + 2);
  243. s->output_out = ff_get_audio_buffer(outlink, s->win_size + 2);
  244. s->output = ff_get_audio_buffer(outlink, s->win_size + 2);
  245. s->overlap_buffer = ff_get_audio_buffer(outlink, s->win_size * 2);
  246. if (!s->overlap_buffer || !s->output || !s->output_out || !s->output_mag ||
  247. !s->output_ph || !s->factors || !s->sfactors)
  248. return AVERROR(ENOMEM);
  249. s->rdft_size = s->win_size / 2 + 1;
  250. s->x_pos = av_calloc(s->rdft_size, sizeof(*s->x_pos));
  251. s->y_pos = av_calloc(s->rdft_size, sizeof(*s->y_pos));
  252. s->l_phase = av_calloc(s->rdft_size, sizeof(*s->l_phase));
  253. s->r_phase = av_calloc(s->rdft_size, sizeof(*s->r_phase));
  254. s->c_mag = av_calloc(s->rdft_size, sizeof(*s->c_mag));
  255. s->c_phase = av_calloc(s->rdft_size, sizeof(*s->c_phase));
  256. s->mag_total = av_calloc(s->rdft_size, sizeof(*s->mag_total));
  257. s->lfe_mag = av_calloc(s->rdft_size, sizeof(*s->lfe_mag));
  258. s->lfe_phase = av_calloc(s->rdft_size, sizeof(*s->lfe_phase));
  259. if (!s->x_pos || !s->y_pos || !s->l_phase || !s->r_phase || !s->lfe_phase ||
  260. !s->c_phase || !s->mag_total || !s->lfe_mag || !s->c_mag)
  261. return AVERROR(ENOMEM);
  262. return 0;
  263. }
  264. static float sqrf(float x)
  265. {
  266. return x * x;
  267. }
  268. static float r_distance(float a)
  269. {
  270. return fminf(sqrtf(1.f + sqrf(tanf(a))), sqrtf(1.f + sqrf(1.f / tanf(a))));
  271. }
  272. #define MIN_MAG_SUM 0.00000001f
  273. static void angle_transform(float *x, float *y, float angle)
  274. {
  275. float reference, r, a;
  276. if (angle == 90.f)
  277. return;
  278. reference = angle * M_PIf / 180.f;
  279. r = hypotf(*x, *y);
  280. a = atan2f(*x, *y);
  281. r /= r_distance(a);
  282. if (fabsf(a) <= M_PI_4f)
  283. a *= reference / M_PI_2f;
  284. else
  285. a = M_PIf + (-2.f * M_PIf + reference) * (M_PIf - fabsf(a)) * FFDIFFSIGN(a, 0.f) / (3.f * M_PI_2f);
  286. r *= r_distance(a);
  287. *x = av_clipf(sinf(a) * r, -1.f, 1.f);
  288. *y = av_clipf(cosf(a) * r, -1.f, 1.f);
  289. }
  290. static void focus_transform(float *x, float *y, float focus)
  291. {
  292. float a, r, ra;
  293. if (focus == 0.f)
  294. return;
  295. a = atan2f(*x, *y);
  296. ra = r_distance(a);
  297. r = av_clipf(hypotf(*x, *y) / ra, 0.f, 1.f);
  298. r = focus > 0.f ? 1.f - powf(1.f - r, 1.f + focus * 20.f) : powf(r, 1.f - focus * 20.f);
  299. r *= ra;
  300. *x = av_clipf(sinf(a) * r, -1.f, 1.f);
  301. *y = av_clipf(cosf(a) * r, -1.f, 1.f);
  302. }
  303. static void stereo_position(float a, float p, float *x, float *y)
  304. {
  305. av_assert2(a >= -1.f && a <= 1.f);
  306. av_assert2(p >= 0.f && p <= M_PIf);
  307. *x = av_clipf(a+a*fmaxf(0.f, p*p-M_PI_2f), -1.f, 1.f);
  308. *y = av_clipf(cosf(a*M_PI_2f+M_PIf)*cosf(M_PI_2f-p/M_PIf)*M_LN10f+1.f, -1.f, 1.f);
  309. }
  310. static inline void get_lfe(int output_lfe, int n, float lowcut, float highcut,
  311. float *lfe_mag, float c_mag, float *mag_total, int lfe_mode)
  312. {
  313. if (output_lfe && n < highcut) {
  314. *lfe_mag = n < lowcut ? 1.f : .5f*(1.f+cosf(M_PIf*(lowcut-n)/(lowcut-highcut)));
  315. *lfe_mag *= c_mag;
  316. if (lfe_mode)
  317. *mag_total -= *lfe_mag;
  318. } else {
  319. *lfe_mag = 0.f;
  320. }
  321. }
  322. #define TRANSFORM \
  323. dst[2 * n ] = mag * cosf(ph); \
  324. dst[2 * n + 1] = mag * sinf(ph);
  325. static void calculate_factors(AVFilterContext *ctx, int ch, int chan)
  326. {
  327. AudioSurroundContext *s = ctx->priv;
  328. float *factor = (float *)s->factors->extended_data[ch];
  329. const float f_x = s->f_x[sc_map[chan >= 0 ? chan : 0]];
  330. const float f_y = s->f_y[sc_map[chan >= 0 ? chan : 0]];
  331. const int rdft_size = s->rdft_size;
  332. const float *x = s->x_pos;
  333. const float *y = s->y_pos;
  334. switch (chan) {
  335. case AV_CHAN_FRONT_CENTER:
  336. for (int n = 0; n < rdft_size; n++)
  337. factor[n] = powf(1.f - fabsf(x[n]), f_x) * powf((y[n] + 1.f) * .5f, f_y);
  338. break;
  339. case AV_CHAN_FRONT_LEFT:
  340. for (int n = 0; n < rdft_size; n++)
  341. factor[n] = powf(.5f * ( x[n] + 1.f), f_x) * powf((y[n] + 1.f) * .5f, f_y);
  342. break;
  343. case AV_CHAN_FRONT_RIGHT:
  344. for (int n = 0; n < rdft_size; n++)
  345. factor[n] = powf(.5f * (-x[n] + 1.f), f_x) * powf((y[n] + 1.f) * .5f, f_y);
  346. break;
  347. case AV_CHAN_LOW_FREQUENCY:
  348. for (int n = 0; n < rdft_size; n++)
  349. factor[n] = powf(1.f - fabsf(x[n]), f_x) * powf((1.f - fabsf(y[n])), f_y);
  350. break;
  351. case AV_CHAN_BACK_CENTER:
  352. for (int n = 0; n < rdft_size; n++)
  353. factor[n] = powf(1.f - fabsf(x[n]), f_x) * powf((1.f - y[n]) * .5f, f_y);
  354. break;
  355. case AV_CHAN_BACK_LEFT:
  356. for (int n = 0; n < rdft_size; n++)
  357. factor[n] = powf(.5f * ( x[n] + 1.f), f_x) * powf(1.f - ((y[n] + 1.f) * .5f), f_y);
  358. break;
  359. case AV_CHAN_BACK_RIGHT:
  360. for (int n = 0; n < rdft_size; n++)
  361. factor[n] = powf(.5f * (-x[n] + 1.f), f_x) * powf(1.f - ((y[n] + 1.f) * .5f), f_y);
  362. break;
  363. case AV_CHAN_SIDE_LEFT:
  364. for (int n = 0; n < rdft_size; n++)
  365. factor[n] = powf(.5f * ( x[n] + 1.f), f_x) * powf(1.f - fabsf(y[n]), f_y);
  366. break;
  367. case AV_CHAN_SIDE_RIGHT:
  368. for (int n = 0; n < rdft_size; n++)
  369. factor[n] = powf(.5f * (-x[n] + 1.f), f_x) * powf(1.f - fabsf(y[n]), f_y);
  370. break;
  371. default:
  372. for (int n = 0; n < rdft_size; n++)
  373. factor[n] = 1.f;
  374. break;
  375. }
  376. }
  377. static void do_transform(AVFilterContext *ctx, int ch)
  378. {
  379. AudioSurroundContext *s = ctx->priv;
  380. float *sfactor = (float *)s->sfactors->extended_data[ch];
  381. float *factor = (float *)s->factors->extended_data[ch];
  382. float *omag = (float *)s->output_mag->extended_data[ch];
  383. float *oph = (float *)s->output_ph->extended_data[ch];
  384. float *dst = (float *)s->output->extended_data[ch];
  385. const int rdft_size = s->rdft_size;
  386. const float smooth = s->smooth;
  387. if (smooth > 0.f) {
  388. for (int n = 0; n < rdft_size; n++)
  389. sfactor[n] = smooth * factor[n] + (1.f - smooth) * sfactor[n];
  390. factor = sfactor;
  391. }
  392. for (int n = 0; n < rdft_size; n++)
  393. omag[n] *= factor[n];
  394. for (int n = 0; n < rdft_size; n++) {
  395. const float mag = omag[n];
  396. const float ph = oph[n];
  397. TRANSFORM
  398. }
  399. }
  400. static void stereo_copy(AVFilterContext *ctx, int ch, int chan)
  401. {
  402. AudioSurroundContext *s = ctx->priv;
  403. float *omag = (float *)s->output_mag->extended_data[ch];
  404. float *oph = (float *)s->output_ph->extended_data[ch];
  405. const float *mag_total = s->mag_total;
  406. const int rdft_size = s->rdft_size;
  407. const float *c_phase = s->c_phase;
  408. const float *l_phase = s->l_phase;
  409. const float *r_phase = s->r_phase;
  410. const float *lfe_mag = s->lfe_mag;
  411. const float *c_mag = s->c_mag;
  412. switch (chan) {
  413. case AV_CHAN_FRONT_CENTER:
  414. memcpy(omag, c_mag, rdft_size * sizeof(*omag));
  415. break;
  416. case AV_CHAN_LOW_FREQUENCY:
  417. memcpy(omag, lfe_mag, rdft_size * sizeof(*omag));
  418. break;
  419. case AV_CHAN_FRONT_LEFT:
  420. case AV_CHAN_FRONT_RIGHT:
  421. case AV_CHAN_BACK_CENTER:
  422. case AV_CHAN_BACK_LEFT:
  423. case AV_CHAN_BACK_RIGHT:
  424. case AV_CHAN_SIDE_LEFT:
  425. case AV_CHAN_SIDE_RIGHT:
  426. memcpy(omag, mag_total, rdft_size * sizeof(*omag));
  427. break;
  428. default:
  429. break;
  430. }
  431. switch (chan) {
  432. case AV_CHAN_FRONT_CENTER:
  433. case AV_CHAN_LOW_FREQUENCY:
  434. case AV_CHAN_BACK_CENTER:
  435. memcpy(oph, c_phase, rdft_size * sizeof(*oph));
  436. break;
  437. case AV_CHAN_FRONT_LEFT:
  438. case AV_CHAN_BACK_LEFT:
  439. case AV_CHAN_SIDE_LEFT:
  440. memcpy(oph, l_phase, rdft_size * sizeof(*oph));
  441. break;
  442. case AV_CHAN_FRONT_RIGHT:
  443. case AV_CHAN_BACK_RIGHT:
  444. case AV_CHAN_SIDE_RIGHT:
  445. memcpy(oph, r_phase, rdft_size * sizeof(*oph));
  446. break;
  447. default:
  448. break;
  449. }
  450. }
  451. static void stereo_upmix(AVFilterContext *ctx, int ch)
  452. {
  453. AudioSurroundContext *s = ctx->priv;
  454. const int chan = av_channel_layout_channel_from_index(&s->out_ch_layout, ch);
  455. calculate_factors(ctx, ch, chan);
  456. stereo_copy(ctx, ch, chan);
  457. do_transform(ctx, ch);
  458. }
  459. static void l2_1_upmix(AVFilterContext *ctx, int ch)
  460. {
  461. AudioSurroundContext *s = ctx->priv;
  462. const int chan = av_channel_layout_channel_from_index(&s->out_ch_layout, ch);
  463. float *omag = (float *)s->output_mag->extended_data[ch];
  464. float *oph = (float *)s->output_ph->extended_data[ch];
  465. const float *mag_total = s->mag_total;
  466. const float *lfe_phase = s->lfe_phase;
  467. const int rdft_size = s->rdft_size;
  468. const float *c_phase = s->c_phase;
  469. const float *l_phase = s->l_phase;
  470. const float *r_phase = s->r_phase;
  471. const float *lfe_mag = s->lfe_mag;
  472. const float *c_mag = s->c_mag;
  473. switch (chan) {
  474. case AV_CHAN_LOW_FREQUENCY:
  475. calculate_factors(ctx, ch, -1);
  476. break;
  477. default:
  478. calculate_factors(ctx, ch, chan);
  479. break;
  480. }
  481. switch (chan) {
  482. case AV_CHAN_FRONT_CENTER:
  483. memcpy(omag, c_mag, rdft_size * sizeof(*omag));
  484. break;
  485. case AV_CHAN_LOW_FREQUENCY:
  486. memcpy(omag, lfe_mag, rdft_size * sizeof(*omag));
  487. break;
  488. case AV_CHAN_FRONT_LEFT:
  489. case AV_CHAN_FRONT_RIGHT:
  490. case AV_CHAN_BACK_CENTER:
  491. case AV_CHAN_BACK_LEFT:
  492. case AV_CHAN_BACK_RIGHT:
  493. case AV_CHAN_SIDE_LEFT:
  494. case AV_CHAN_SIDE_RIGHT:
  495. memcpy(omag, mag_total, rdft_size * sizeof(*omag));
  496. break;
  497. default:
  498. break;
  499. }
  500. switch (chan) {
  501. case AV_CHAN_LOW_FREQUENCY:
  502. memcpy(oph, lfe_phase, rdft_size * sizeof(*oph));
  503. break;
  504. case AV_CHAN_FRONT_CENTER:
  505. case AV_CHAN_BACK_CENTER:
  506. memcpy(oph, c_phase, rdft_size * sizeof(*oph));
  507. break;
  508. case AV_CHAN_FRONT_LEFT:
  509. case AV_CHAN_BACK_LEFT:
  510. case AV_CHAN_SIDE_LEFT:
  511. memcpy(oph, l_phase, rdft_size * sizeof(*oph));
  512. break;
  513. case AV_CHAN_FRONT_RIGHT:
  514. case AV_CHAN_BACK_RIGHT:
  515. case AV_CHAN_SIDE_RIGHT:
  516. memcpy(oph, r_phase, rdft_size * sizeof(*oph));
  517. break;
  518. default:
  519. break;
  520. }
  521. do_transform(ctx, ch);
  522. }
  523. static void surround_upmix(AVFilterContext *ctx, int ch)
  524. {
  525. AudioSurroundContext *s = ctx->priv;
  526. const int chan = av_channel_layout_channel_from_index(&s->out_ch_layout, ch);
  527. switch (chan) {
  528. case AV_CHAN_FRONT_CENTER:
  529. calculate_factors(ctx, ch, -1);
  530. break;
  531. default:
  532. calculate_factors(ctx, ch, chan);
  533. break;
  534. }
  535. stereo_copy(ctx, ch, chan);
  536. do_transform(ctx, ch);
  537. }
  538. static void upmix_7_1_5_0_side(AVFilterContext *ctx,
  539. float c_re, float c_im,
  540. float mag_totall, float mag_totalr,
  541. float fl_phase, float fr_phase,
  542. float bl_phase, float br_phase,
  543. float sl_phase, float sr_phase,
  544. float xl, float yl,
  545. float xr, float yr,
  546. int n)
  547. {
  548. float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
  549. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  550. float lfe_mag, c_phase, mag_total = (mag_totall + mag_totalr) * 0.5f;
  551. AudioSurroundContext *s = ctx->priv;
  552. dstl = (float *)s->output->extended_data[0];
  553. dstr = (float *)s->output->extended_data[1];
  554. dstc = (float *)s->output->extended_data[2];
  555. dstlfe = (float *)s->output->extended_data[3];
  556. dstlb = (float *)s->output->extended_data[4];
  557. dstrb = (float *)s->output->extended_data[5];
  558. dstls = (float *)s->output->extended_data[6];
  559. dstrs = (float *)s->output->extended_data[7];
  560. c_phase = atan2f(c_im, c_re);
  561. get_lfe(s->output_lfe, n, s->lowcut, s->highcut, &lfe_mag, hypotf(c_re, c_im), &mag_total, s->lfe_mode);
  562. fl_mag = powf(.5f * (xl + 1.f), s->f_x[SC_FL]) * powf((yl + 1.f) * .5f, s->f_y[SC_FL]) * mag_totall;
  563. fr_mag = powf(.5f * (xr + 1.f), s->f_x[SC_FR]) * powf((yr + 1.f) * .5f, s->f_y[SC_FR]) * mag_totalr;
  564. lb_mag = powf(.5f * (-xl + 1.f), s->f_x[SC_BL]) * powf((yl + 1.f) * .5f, s->f_y[SC_BL]) * mag_totall;
  565. rb_mag = powf(.5f * (-xr + 1.f), s->f_x[SC_BR]) * powf((yr + 1.f) * .5f, s->f_y[SC_BR]) * mag_totalr;
  566. ls_mag = powf(1.f - fabsf(xl), s->f_x[SC_SL]) * powf((yl + 1.f) * .5f, s->f_y[SC_SL]) * mag_totall;
  567. rs_mag = powf(1.f - fabsf(xr), s->f_x[SC_SR]) * powf((yr + 1.f) * .5f, s->f_y[SC_SR]) * mag_totalr;
  568. dstl[2 * n ] = fl_mag * cosf(fl_phase);
  569. dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
  570. dstr[2 * n ] = fr_mag * cosf(fr_phase);
  571. dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
  572. dstc[2 * n ] = c_re;
  573. dstc[2 * n + 1] = c_im;
  574. dstlfe[2 * n ] = lfe_mag * cosf(c_phase);
  575. dstlfe[2 * n + 1] = lfe_mag * sinf(c_phase);
  576. dstlb[2 * n ] = lb_mag * cosf(bl_phase);
  577. dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
  578. dstrb[2 * n ] = rb_mag * cosf(br_phase);
  579. dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
  580. dstls[2 * n ] = ls_mag * cosf(sl_phase);
  581. dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
  582. dstrs[2 * n ] = rs_mag * cosf(sr_phase);
  583. dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
  584. }
  585. static void upmix_7_1_5_1(AVFilterContext *ctx,
  586. float c_re, float c_im,
  587. float lfe_re, float lfe_im,
  588. float mag_totall, float mag_totalr,
  589. float fl_phase, float fr_phase,
  590. float bl_phase, float br_phase,
  591. float sl_phase, float sr_phase,
  592. float xl, float yl,
  593. float xr, float yr,
  594. int n)
  595. {
  596. float fl_mag, fr_mag, ls_mag, rs_mag, lb_mag, rb_mag;
  597. float *dstc, *dstl, *dstr, *dstls, *dstrs, *dstlb, *dstrb, *dstlfe;
  598. AudioSurroundContext *s = ctx->priv;
  599. dstl = (float *)s->output->extended_data[0];
  600. dstr = (float *)s->output->extended_data[1];
  601. dstc = (float *)s->output->extended_data[2];
  602. dstlfe = (float *)s->output->extended_data[3];
  603. dstlb = (float *)s->output->extended_data[4];
  604. dstrb = (float *)s->output->extended_data[5];
  605. dstls = (float *)s->output->extended_data[6];
  606. dstrs = (float *)s->output->extended_data[7];
  607. fl_mag = powf(.5f * (xl + 1.f), s->f_x[SC_FL]) * powf((yl + 1.f) * .5f, s->f_y[SC_FL]) * mag_totall;
  608. fr_mag = powf(.5f * (xr + 1.f), s->f_x[SC_FR]) * powf((yr + 1.f) * .5f, s->f_y[SC_FR]) * mag_totalr;
  609. lb_mag = powf(.5f * (-xl + 1.f), s->f_x[SC_BL]) * powf((yl + 1.f) * .5f, s->f_y[SC_BL]) * mag_totall;
  610. rb_mag = powf(.5f * (-xr + 1.f), s->f_x[SC_BR]) * powf((yr + 1.f) * .5f, s->f_y[SC_BR]) * mag_totalr;
  611. ls_mag = powf(1.f - fabsf(xl), s->f_x[SC_SL]) * powf((yl + 1.f) * .5f, s->f_y[SC_SL]) * mag_totall;
  612. rs_mag = powf(1.f - fabsf(xr), s->f_x[SC_SR]) * powf((yr + 1.f) * .5f, s->f_y[SC_SR]) * mag_totalr;
  613. dstl[2 * n ] = fl_mag * cosf(fl_phase);
  614. dstl[2 * n + 1] = fl_mag * sinf(fl_phase);
  615. dstr[2 * n ] = fr_mag * cosf(fr_phase);
  616. dstr[2 * n + 1] = fr_mag * sinf(fr_phase);
  617. dstc[2 * n ] = c_re;
  618. dstc[2 * n + 1] = c_im;
  619. dstlfe[2 * n ] = lfe_re;
  620. dstlfe[2 * n + 1] = lfe_im;
  621. dstlb[2 * n ] = lb_mag * cosf(bl_phase);
  622. dstlb[2 * n + 1] = lb_mag * sinf(bl_phase);
  623. dstrb[2 * n ] = rb_mag * cosf(br_phase);
  624. dstrb[2 * n + 1] = rb_mag * sinf(br_phase);
  625. dstls[2 * n ] = ls_mag * cosf(sl_phase);
  626. dstls[2 * n + 1] = ls_mag * sinf(sl_phase);
  627. dstrs[2 * n ] = rs_mag * cosf(sr_phase);
  628. dstrs[2 * n + 1] = rs_mag * sinf(sr_phase);
  629. }
  630. static void filter_stereo(AVFilterContext *ctx)
  631. {
  632. AudioSurroundContext *s = ctx->priv;
  633. const float *srcl = (const float *)s->input->extended_data[0];
  634. const float *srcr = (const float *)s->input->extended_data[1];
  635. const int output_lfe = s->output_lfe && s->create_lfe;
  636. const int rdft_size = s->rdft_size;
  637. const int lfe_mode = s->lfe_mode;
  638. const float highcut = s->highcut;
  639. const float lowcut = s->lowcut;
  640. const float angle = s->angle;
  641. const float focus = s->focus;
  642. float *magtotal = s->mag_total;
  643. float *lfemag = s->lfe_mag;
  644. float *lphase = s->l_phase;
  645. float *rphase = s->r_phase;
  646. float *cphase = s->c_phase;
  647. float *cmag = s->c_mag;
  648. float *xpos = s->x_pos;
  649. float *ypos = s->y_pos;
  650. for (int n = 0; n < rdft_size; n++) {
  651. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  652. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  653. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  654. float l_mag = hypotf(l_re, l_im);
  655. float r_mag = hypotf(r_re, r_im);
  656. float mag_total = hypotf(l_mag, r_mag);
  657. float l_phase = atan2f(l_im, l_re);
  658. float r_phase = atan2f(r_im, r_re);
  659. float phase_dif = fabsf(l_phase - r_phase);
  660. float mag_sum = l_mag + r_mag;
  661. float c_mag = mag_sum * 0.5f;
  662. float mag_dif, x, y;
  663. mag_sum = mag_sum < MIN_MAG_SUM ? 1.f : mag_sum;
  664. mag_dif = (l_mag - r_mag) / mag_sum;
  665. if (phase_dif > M_PIf)
  666. phase_dif = 2.f * M_PIf - phase_dif;
  667. stereo_position(mag_dif, phase_dif, &x, &y);
  668. angle_transform(&x, &y, angle);
  669. focus_transform(&x, &y, focus);
  670. get_lfe(output_lfe, n, lowcut, highcut, &lfemag[n], c_mag, &mag_total, lfe_mode);
  671. xpos[n] = x;
  672. ypos[n] = y;
  673. lphase[n] = l_phase;
  674. rphase[n] = r_phase;
  675. cmag[n] = c_mag;
  676. cphase[n] = c_phase;
  677. magtotal[n] = mag_total;
  678. }
  679. }
  680. static void filter_2_1(AVFilterContext *ctx)
  681. {
  682. AudioSurroundContext *s = ctx->priv;
  683. const float *srcl = (const float *)s->input->extended_data[0];
  684. const float *srcr = (const float *)s->input->extended_data[1];
  685. const float *srclfe = (const float *)s->input->extended_data[2];
  686. const int rdft_size = s->rdft_size;
  687. const float angle = s->angle;
  688. const float focus = s->focus;
  689. float *magtotal = s->mag_total;
  690. float *lfephase = s->lfe_phase;
  691. float *lfemag = s->lfe_mag;
  692. float *lphase = s->l_phase;
  693. float *rphase = s->r_phase;
  694. float *cphase = s->c_phase;
  695. float *cmag = s->c_mag;
  696. float *xpos = s->x_pos;
  697. float *ypos = s->y_pos;
  698. for (int n = 0; n < rdft_size; n++) {
  699. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  700. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  701. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  702. float c_phase = atan2f(l_im + r_im, l_re + r_re);
  703. float l_mag = hypotf(l_re, l_im);
  704. float r_mag = hypotf(r_re, r_im);
  705. float lfe_mag = hypotf(lfe_re, lfe_im);
  706. float lfe_phase = atan2f(lfe_im, lfe_re);
  707. float mag_total = hypotf(l_mag, r_mag);
  708. float l_phase = atan2f(l_im, l_re);
  709. float r_phase = atan2f(r_im, r_re);
  710. float phase_dif = fabsf(l_phase - r_phase);
  711. float mag_sum = l_mag + r_mag;
  712. float c_mag = mag_sum * 0.5f;
  713. float mag_dif, x, y;
  714. mag_sum = mag_sum < MIN_MAG_SUM ? 1.f : mag_sum;
  715. mag_dif = (l_mag - r_mag) / mag_sum;
  716. if (phase_dif > M_PIf)
  717. phase_dif = 2.f * M_PIf - phase_dif;
  718. stereo_position(mag_dif, phase_dif, &x, &y);
  719. angle_transform(&x, &y, angle);
  720. focus_transform(&x, &y, focus);
  721. xpos[n] = x;
  722. ypos[n] = y;
  723. lphase[n] = l_phase;
  724. rphase[n] = r_phase;
  725. cmag[n] = c_mag;
  726. cphase[n] = c_phase;
  727. lfemag[n] = lfe_mag;
  728. lfephase[n] = lfe_phase;
  729. magtotal[n] = mag_total;
  730. }
  731. }
  732. static void filter_surround(AVFilterContext *ctx)
  733. {
  734. AudioSurroundContext *s = ctx->priv;
  735. const float *srcl = (const float *)s->input->extended_data[0];
  736. const float *srcr = (const float *)s->input->extended_data[1];
  737. const float *srcc = (const float *)s->input->extended_data[2];
  738. const int output_lfe = s->output_lfe && s->create_lfe;
  739. const int rdft_size = s->rdft_size;
  740. const int lfe_mode = s->lfe_mode;
  741. const float highcut = s->highcut;
  742. const float lowcut = s->lowcut;
  743. const float angle = s->angle;
  744. const float focus = s->focus;
  745. float *magtotal = s->mag_total;
  746. float *lfemag = s->lfe_mag;
  747. float *lphase = s->l_phase;
  748. float *rphase = s->r_phase;
  749. float *cphase = s->c_phase;
  750. float *cmag = s->c_mag;
  751. float *xpos = s->x_pos;
  752. float *ypos = s->y_pos;
  753. for (int n = 0; n < rdft_size; n++) {
  754. float l_re = srcl[2 * n], r_re = srcr[2 * n];
  755. float l_im = srcl[2 * n + 1], r_im = srcr[2 * n + 1];
  756. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  757. float c_phase = atan2f(c_im, c_re);
  758. float c_mag = hypotf(c_re, c_im);
  759. float l_mag = hypotf(l_re, l_im);
  760. float r_mag = hypotf(r_re, r_im);
  761. float mag_total = hypotf(l_mag, r_mag);
  762. float l_phase = atan2f(l_im, l_re);
  763. float r_phase = atan2f(r_im, r_re);
  764. float phase_dif = fabsf(l_phase - r_phase);
  765. float mag_sum = l_mag + r_mag;
  766. float mag_dif, x, y;
  767. mag_sum = mag_sum < MIN_MAG_SUM ? 1.f : mag_sum;
  768. mag_dif = (l_mag - r_mag) / mag_sum;
  769. if (phase_dif > M_PIf)
  770. phase_dif = 2.f * M_PIf - phase_dif;
  771. stereo_position(mag_dif, phase_dif, &x, &y);
  772. angle_transform(&x, &y, angle);
  773. focus_transform(&x, &y, focus);
  774. get_lfe(output_lfe, n, lowcut, highcut, &lfemag[n], c_mag, &mag_total, lfe_mode);
  775. xpos[n] = x;
  776. ypos[n] = y;
  777. lphase[n] = l_phase;
  778. rphase[n] = r_phase;
  779. cmag[n] = c_mag;
  780. cphase[n] = c_phase;
  781. magtotal[n] = mag_total;
  782. }
  783. }
  784. static void filter_5_0_side(AVFilterContext *ctx)
  785. {
  786. AudioSurroundContext *s = ctx->priv;
  787. const int rdft_size = s->rdft_size;
  788. float *srcl, *srcr, *srcc, *srcsl, *srcsr;
  789. int n;
  790. srcl = (float *)s->input->extended_data[0];
  791. srcr = (float *)s->input->extended_data[1];
  792. srcc = (float *)s->input->extended_data[2];
  793. srcsl = (float *)s->input->extended_data[3];
  794. srcsr = (float *)s->input->extended_data[4];
  795. for (n = 0; n < rdft_size; n++) {
  796. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  797. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  798. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  799. float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
  800. float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
  801. float fl_mag = hypotf(fl_re, fl_im);
  802. float fr_mag = hypotf(fr_re, fr_im);
  803. float fl_phase = atan2f(fl_im, fl_re);
  804. float fr_phase = atan2f(fr_im, fr_re);
  805. float sl_mag = hypotf(sl_re, sl_im);
  806. float sr_mag = hypotf(sr_re, sr_im);
  807. float sl_phase = atan2f(sl_im, sl_re);
  808. float sr_phase = atan2f(sr_im, sr_re);
  809. float phase_difl = fabsf(fl_phase - sl_phase);
  810. float phase_difr = fabsf(fr_phase - sr_phase);
  811. float magl_sum = fl_mag + sl_mag;
  812. float magr_sum = fr_mag + sr_mag;
  813. float mag_difl = magl_sum < MIN_MAG_SUM ? FFDIFFSIGN(fl_mag, sl_mag) : (fl_mag - sl_mag) / magl_sum;
  814. float mag_difr = magr_sum < MIN_MAG_SUM ? FFDIFFSIGN(fr_mag, sr_mag) : (fr_mag - sr_mag) / magr_sum;
  815. float mag_totall = hypotf(fl_mag, sl_mag);
  816. float mag_totalr = hypotf(fr_mag, sr_mag);
  817. float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
  818. float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
  819. float xl, yl;
  820. float xr, yr;
  821. if (phase_difl > M_PIf)
  822. phase_difl = 2.f * M_PIf - phase_difl;
  823. if (phase_difr > M_PIf)
  824. phase_difr = 2.f * M_PIf - phase_difr;
  825. stereo_position(mag_difl, phase_difl, &xl, &yl);
  826. stereo_position(mag_difr, phase_difr, &xr, &yr);
  827. s->upmix_5_0(ctx, c_re, c_im,
  828. mag_totall, mag_totalr,
  829. fl_phase, fr_phase,
  830. bl_phase, br_phase,
  831. sl_phase, sr_phase,
  832. xl, yl, xr, yr, n);
  833. }
  834. }
  835. static void filter_5_1_side(AVFilterContext *ctx)
  836. {
  837. AudioSurroundContext *s = ctx->priv;
  838. const int rdft_size = s->rdft_size;
  839. float *srcl, *srcr, *srcc, *srclfe, *srcsl, *srcsr;
  840. int n;
  841. srcl = (float *)s->input->extended_data[0];
  842. srcr = (float *)s->input->extended_data[1];
  843. srcc = (float *)s->input->extended_data[2];
  844. srclfe = (float *)s->input->extended_data[3];
  845. srcsl = (float *)s->input->extended_data[4];
  846. srcsr = (float *)s->input->extended_data[5];
  847. for (n = 0; n < rdft_size; n++) {
  848. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  849. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  850. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  851. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  852. float sl_re = srcsl[2 * n], sl_im = srcsl[2 * n + 1];
  853. float sr_re = srcsr[2 * n], sr_im = srcsr[2 * n + 1];
  854. float fl_mag = hypotf(fl_re, fl_im);
  855. float fr_mag = hypotf(fr_re, fr_im);
  856. float fl_phase = atan2f(fl_im, fl_re);
  857. float fr_phase = atan2f(fr_im, fr_re);
  858. float sl_mag = hypotf(sl_re, sl_im);
  859. float sr_mag = hypotf(sr_re, sr_im);
  860. float sl_phase = atan2f(sl_im, sl_re);
  861. float sr_phase = atan2f(sr_im, sr_re);
  862. float phase_difl = fabsf(fl_phase - sl_phase);
  863. float phase_difr = fabsf(fr_phase - sr_phase);
  864. float magl_sum = fl_mag + sl_mag;
  865. float magr_sum = fr_mag + sr_mag;
  866. float mag_difl = magl_sum < MIN_MAG_SUM ? FFDIFFSIGN(fl_mag, sl_mag) : (fl_mag - sl_mag) / magl_sum;
  867. float mag_difr = magr_sum < MIN_MAG_SUM ? FFDIFFSIGN(fr_mag, sr_mag) : (fr_mag - sr_mag) / magr_sum;
  868. float mag_totall = hypotf(fl_mag, sl_mag);
  869. float mag_totalr = hypotf(fr_mag, sr_mag);
  870. float bl_phase = atan2f(fl_im + sl_im, fl_re + sl_re);
  871. float br_phase = atan2f(fr_im + sr_im, fr_re + sr_re);
  872. float xl, yl;
  873. float xr, yr;
  874. if (phase_difl > M_PIf)
  875. phase_difl = 2.f * M_PIf - phase_difl;
  876. if (phase_difr > M_PIf)
  877. phase_difr = 2.f * M_PIf - phase_difr;
  878. stereo_position(mag_difl, phase_difl, &xl, &yl);
  879. stereo_position(mag_difr, phase_difr, &xr, &yr);
  880. s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
  881. mag_totall, mag_totalr,
  882. fl_phase, fr_phase,
  883. bl_phase, br_phase,
  884. sl_phase, sr_phase,
  885. xl, yl, xr, yr, n);
  886. }
  887. }
  888. static void filter_5_1_back(AVFilterContext *ctx)
  889. {
  890. AudioSurroundContext *s = ctx->priv;
  891. const int rdft_size = s->rdft_size;
  892. float *srcl, *srcr, *srcc, *srclfe, *srcbl, *srcbr;
  893. int n;
  894. srcl = (float *)s->input->extended_data[0];
  895. srcr = (float *)s->input->extended_data[1];
  896. srcc = (float *)s->input->extended_data[2];
  897. srclfe = (float *)s->input->extended_data[3];
  898. srcbl = (float *)s->input->extended_data[4];
  899. srcbr = (float *)s->input->extended_data[5];
  900. for (n = 0; n < rdft_size; n++) {
  901. float fl_re = srcl[2 * n], fr_re = srcr[2 * n];
  902. float fl_im = srcl[2 * n + 1], fr_im = srcr[2 * n + 1];
  903. float c_re = srcc[2 * n], c_im = srcc[2 * n + 1];
  904. float lfe_re = srclfe[2 * n], lfe_im = srclfe[2 * n + 1];
  905. float bl_re = srcbl[2 * n], bl_im = srcbl[2 * n + 1];
  906. float br_re = srcbr[2 * n], br_im = srcbr[2 * n + 1];
  907. float fl_mag = hypotf(fl_re, fl_im);
  908. float fr_mag = hypotf(fr_re, fr_im);
  909. float fl_phase = atan2f(fl_im, fl_re);
  910. float fr_phase = atan2f(fr_im, fr_re);
  911. float bl_mag = hypotf(bl_re, bl_im);
  912. float br_mag = hypotf(br_re, br_im);
  913. float bl_phase = atan2f(bl_im, bl_re);
  914. float br_phase = atan2f(br_im, br_re);
  915. float phase_difl = fabsf(fl_phase - bl_phase);
  916. float phase_difr = fabsf(fr_phase - br_phase);
  917. float magl_sum = fl_mag + bl_mag;
  918. float magr_sum = fr_mag + br_mag;
  919. float mag_difl = magl_sum < MIN_MAG_SUM ? FFDIFFSIGN(fl_mag, bl_mag) : (fl_mag - bl_mag) / magl_sum;
  920. float mag_difr = magr_sum < MIN_MAG_SUM ? FFDIFFSIGN(fr_mag, br_mag) : (fr_mag - br_mag) / magr_sum;
  921. float mag_totall = hypotf(fl_mag, bl_mag);
  922. float mag_totalr = hypotf(fr_mag, br_mag);
  923. float sl_phase = atan2f(fl_im + bl_im, fl_re + bl_re);
  924. float sr_phase = atan2f(fr_im + br_im, fr_re + br_re);
  925. float xl, yl;
  926. float xr, yr;
  927. if (phase_difl > M_PIf)
  928. phase_difl = 2.f * M_PIf - phase_difl;
  929. if (phase_difr > M_PIf)
  930. phase_difr = 2.f * M_PIf - phase_difr;
  931. stereo_position(mag_difl, phase_difl, &xl, &yl);
  932. stereo_position(mag_difr, phase_difr, &xr, &yr);
  933. s->upmix_5_1(ctx, c_re, c_im, lfe_re, lfe_im,
  934. mag_totall, mag_totalr,
  935. fl_phase, fr_phase,
  936. bl_phase, br_phase,
  937. sl_phase, sr_phase,
  938. xl, yl, xr, yr, n);
  939. }
  940. }
  941. static void allchannels_spread(AVFilterContext *ctx)
  942. {
  943. AudioSurroundContext *s = ctx->priv;
  944. if (s->all_x >= 0.f)
  945. for (int n = 0; n < SC_NB; n++)
  946. s->f_x[n] = s->all_x;
  947. s->all_x = -1.f;
  948. if (s->all_y >= 0.f)
  949. for (int n = 0; n < SC_NB; n++)
  950. s->f_y[n] = s->all_y;
  951. s->all_y = -1.f;
  952. }
  953. static av_cold int init(AVFilterContext *ctx)
  954. {
  955. AudioSurroundContext *s = ctx->priv;
  956. int64_t in_channel_layout, out_channel_layout;
  957. float overlap;
  958. int ret;
  959. if ((ret = av_channel_layout_from_string(&s->out_ch_layout, s->out_channel_layout_str)) < 0) {
  960. av_log(ctx, AV_LOG_ERROR, "Error parsing output channel layout '%s'.\n",
  961. s->out_channel_layout_str);
  962. return ret;
  963. }
  964. if ((ret = av_channel_layout_from_string(&s->in_ch_layout, s->in_channel_layout_str)) < 0) {
  965. av_log(ctx, AV_LOG_ERROR, "Error parsing input channel layout '%s'.\n",
  966. s->in_channel_layout_str);
  967. return AVERROR(EINVAL);
  968. }
  969. if (s->lowcutf >= s->highcutf) {
  970. av_log(ctx, AV_LOG_ERROR, "Low cut-off '%d' should be less than high cut-off '%d'.\n",
  971. s->lowcutf, s->highcutf);
  972. return AVERROR(EINVAL);
  973. }
  974. in_channel_layout = s->in_ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
  975. s->in_ch_layout.u.mask : 0;
  976. out_channel_layout = s->out_ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
  977. s->out_ch_layout.u.mask : 0;
  978. s->create_lfe = av_channel_layout_index_from_channel(&s->out_ch_layout,
  979. AV_CHAN_LOW_FREQUENCY) >= 0;
  980. switch (in_channel_layout) {
  981. case AV_CH_LAYOUT_STEREO:
  982. s->filter = filter_stereo;
  983. s->upmix = stereo_upmix;
  984. break;
  985. case AV_CH_LAYOUT_2POINT1:
  986. s->filter = filter_2_1;
  987. s->upmix = l2_1_upmix;
  988. break;
  989. case AV_CH_LAYOUT_SURROUND:
  990. s->filter = filter_surround;
  991. s->upmix = surround_upmix;
  992. break;
  993. case AV_CH_LAYOUT_5POINT0:
  994. s->filter = filter_5_0_side;
  995. switch (out_channel_layout) {
  996. case AV_CH_LAYOUT_7POINT1:
  997. s->upmix_5_0 = upmix_7_1_5_0_side;
  998. break;
  999. default:
  1000. goto fail;
  1001. }
  1002. break;
  1003. case AV_CH_LAYOUT_5POINT1:
  1004. s->filter = filter_5_1_side;
  1005. switch (out_channel_layout) {
  1006. case AV_CH_LAYOUT_7POINT1:
  1007. s->upmix_5_1 = upmix_7_1_5_1;
  1008. break;
  1009. default:
  1010. goto fail;
  1011. }
  1012. break;
  1013. case AV_CH_LAYOUT_5POINT1_BACK:
  1014. s->filter = filter_5_1_back;
  1015. switch (out_channel_layout) {
  1016. case AV_CH_LAYOUT_7POINT1:
  1017. s->upmix_5_1 = upmix_7_1_5_1;
  1018. break;
  1019. default:
  1020. goto fail;
  1021. }
  1022. break;
  1023. default:
  1024. fail:
  1025. av_log(ctx, AV_LOG_ERROR, "Unsupported upmix: '%s' -> '%s'.\n",
  1026. s->in_channel_layout_str, s->out_channel_layout_str);
  1027. return AVERROR(EINVAL);
  1028. }
  1029. s->window_func_lut = av_calloc(s->win_size, sizeof(*s->window_func_lut));
  1030. if (!s->window_func_lut)
  1031. return AVERROR(ENOMEM);
  1032. generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
  1033. if (s->overlap == 1)
  1034. s->overlap = overlap;
  1035. for (int i = 0; i < s->win_size; i++)
  1036. s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->win_size);
  1037. s->hop_size = FFMAX(1, s->win_size * (1. - s->overlap));
  1038. {
  1039. float max = 0.f, *temp_lut = av_calloc(s->win_size, sizeof(*temp_lut));
  1040. if (!temp_lut)
  1041. return AVERROR(ENOMEM);
  1042. for (int j = 0; j < s->win_size; j += s->hop_size) {
  1043. for (int i = 0; i < s->win_size; i++)
  1044. temp_lut[(i + j) % s->win_size] += s->window_func_lut[i];
  1045. }
  1046. for (int i = 0; i < s->win_size; i++)
  1047. max = fmaxf(temp_lut[i], max);
  1048. av_freep(&temp_lut);
  1049. s->win_gain = 1.f / (max * sqrtf(s->win_size));
  1050. }
  1051. allchannels_spread(ctx);
  1052. return 0;
  1053. }
  1054. static int fft_channel(AVFilterContext *ctx, AVFrame *in, int ch)
  1055. {
  1056. AudioSurroundContext *s = ctx->priv;
  1057. float *src = (float *)s->input_in->extended_data[ch];
  1058. float *win = (float *)s->window->extended_data[ch];
  1059. const int offset = s->win_size - s->hop_size;
  1060. const float level_in = s->input_levels[ch];
  1061. memmove(src, &src[s->hop_size], offset * sizeof(float));
  1062. memcpy(&src[offset], in->extended_data[ch], in->nb_samples * sizeof(float));
  1063. memset(&src[offset + in->nb_samples], 0, (s->hop_size - in->nb_samples) * sizeof(float));
  1064. for (int n = 0; n < s->win_size; n++)
  1065. win[n] = src[n] * s->window_func_lut[n] * level_in;
  1066. s->tx_fn(s->rdft[ch], (float *)s->input->extended_data[ch], win, sizeof(float));
  1067. return 0;
  1068. }
  1069. static int fft_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  1070. {
  1071. AVFrame *in = arg;
  1072. const int start = (in->ch_layout.nb_channels * jobnr) / nb_jobs;
  1073. const int end = (in->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
  1074. for (int ch = start; ch < end; ch++)
  1075. fft_channel(ctx, in, ch);
  1076. return 0;
  1077. }
  1078. static int ifft_channel(AVFilterContext *ctx, AVFrame *out, int ch)
  1079. {
  1080. AudioSurroundContext *s = ctx->priv;
  1081. const float level_out = s->output_levels[ch] * s->win_gain;
  1082. float *dst, *ptr;
  1083. dst = (float *)s->output_out->extended_data[ch];
  1084. ptr = (float *)s->overlap_buffer->extended_data[ch];
  1085. s->itx_fn(s->irdft[ch], dst, (float *)s->output->extended_data[ch], sizeof(AVComplexFloat));
  1086. memmove(s->overlap_buffer->extended_data[ch],
  1087. s->overlap_buffer->extended_data[ch] + s->hop_size * sizeof(float),
  1088. s->win_size * sizeof(float));
  1089. memset(s->overlap_buffer->extended_data[ch] + s->win_size * sizeof(float),
  1090. 0, s->hop_size * sizeof(float));
  1091. for (int n = 0; n < s->win_size; n++)
  1092. ptr[n] += dst[n] * s->window_func_lut[n] * level_out;
  1093. ptr = (float *)s->overlap_buffer->extended_data[ch];
  1094. dst = (float *)out->extended_data[ch];
  1095. memcpy(dst, ptr, s->hop_size * sizeof(float));
  1096. return 0;
  1097. }
  1098. static int ifft_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  1099. {
  1100. AudioSurroundContext *s = ctx->priv;
  1101. AVFrame *out = arg;
  1102. const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
  1103. const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
  1104. for (int ch = start; ch < end; ch++) {
  1105. if (s->upmix)
  1106. s->upmix(ctx, ch);
  1107. ifft_channel(ctx, out, ch);
  1108. }
  1109. return 0;
  1110. }
  1111. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  1112. {
  1113. AVFilterContext *ctx = inlink->dst;
  1114. AVFilterLink *outlink = ctx->outputs[0];
  1115. AudioSurroundContext *s = ctx->priv;
  1116. AVFrame *out;
  1117. ff_filter_execute(ctx, fft_channels, in, NULL,
  1118. FFMIN(inlink->ch_layout.nb_channels,
  1119. ff_filter_get_nb_threads(ctx)));
  1120. s->filter(ctx);
  1121. out = ff_get_audio_buffer(outlink, s->hop_size);
  1122. if (!out)
  1123. return AVERROR(ENOMEM);
  1124. ff_filter_execute(ctx, ifft_channels, out, NULL,
  1125. FFMIN(outlink->ch_layout.nb_channels,
  1126. ff_filter_get_nb_threads(ctx)));
  1127. av_frame_copy_props(out, in);
  1128. out->nb_samples = in->nb_samples;
  1129. av_frame_free(&in);
  1130. return ff_filter_frame(outlink, out);
  1131. }
  1132. static int activate(AVFilterContext *ctx)
  1133. {
  1134. AVFilterLink *inlink = ctx->inputs[0];
  1135. AVFilterLink *outlink = ctx->outputs[0];
  1136. AudioSurroundContext *s = ctx->priv;
  1137. AVFrame *in = NULL;
  1138. int ret = 0, status;
  1139. int64_t pts;
  1140. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  1141. ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &in);
  1142. if (ret < 0)
  1143. return ret;
  1144. if (ret > 0)
  1145. ret = filter_frame(inlink, in);
  1146. if (ret < 0)
  1147. return ret;
  1148. if (ff_inlink_queued_samples(inlink) >= s->hop_size) {
  1149. ff_filter_set_ready(ctx, 10);
  1150. return 0;
  1151. }
  1152. if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
  1153. ff_outlink_set_status(outlink, status, pts);
  1154. return 0;
  1155. }
  1156. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  1157. return FFERROR_NOT_READY;
  1158. }
  1159. static av_cold void uninit(AVFilterContext *ctx)
  1160. {
  1161. AudioSurroundContext *s = ctx->priv;
  1162. av_frame_free(&s->factors);
  1163. av_frame_free(&s->sfactors);
  1164. av_frame_free(&s->window);
  1165. av_frame_free(&s->input_in);
  1166. av_frame_free(&s->input);
  1167. av_frame_free(&s->output);
  1168. av_frame_free(&s->output_ph);
  1169. av_frame_free(&s->output_mag);
  1170. av_frame_free(&s->output_out);
  1171. av_frame_free(&s->overlap_buffer);
  1172. for (int ch = 0; ch < s->nb_in_channels; ch++)
  1173. av_tx_uninit(&s->rdft[ch]);
  1174. for (int ch = 0; ch < s->nb_out_channels; ch++)
  1175. av_tx_uninit(&s->irdft[ch]);
  1176. av_freep(&s->input_levels);
  1177. av_freep(&s->output_levels);
  1178. av_freep(&s->rdft);
  1179. av_freep(&s->irdft);
  1180. av_freep(&s->window_func_lut);
  1181. av_freep(&s->x_pos);
  1182. av_freep(&s->y_pos);
  1183. av_freep(&s->l_phase);
  1184. av_freep(&s->r_phase);
  1185. av_freep(&s->c_mag);
  1186. av_freep(&s->c_phase);
  1187. av_freep(&s->mag_total);
  1188. av_freep(&s->lfe_mag);
  1189. av_freep(&s->lfe_phase);
  1190. }
  1191. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  1192. char *res, int res_len, int flags)
  1193. {
  1194. AudioSurroundContext *s = ctx->priv;
  1195. int ret;
  1196. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  1197. if (ret < 0)
  1198. return ret;
  1199. s->hop_size = FFMAX(1, s->win_size * (1. - s->overlap));
  1200. allchannels_spread(ctx);
  1201. set_input_levels(ctx);
  1202. set_output_levels(ctx);
  1203. return 0;
  1204. }
  1205. #define OFFSET(x) offsetof(AudioSurroundContext, x)
  1206. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  1207. #define TFLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  1208. static const AVOption surround_options[] = {
  1209. { "chl_out", "set output channel layout", OFFSET(out_channel_layout_str), AV_OPT_TYPE_STRING, {.str="5.1"}, 0, 0, FLAGS },
  1210. { "chl_in", "set input channel layout", OFFSET(in_channel_layout_str), AV_OPT_TYPE_STRING, {.str="stereo"},0, 0, FLAGS },
  1211. { "level_in", "set input level", OFFSET(level_in), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1212. { "level_out", "set output level", OFFSET(level_out), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1213. { "lfe", "output LFE", OFFSET(output_lfe), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, TFLAGS },
  1214. { "lfe_low", "LFE low cut off", OFFSET(lowcutf), AV_OPT_TYPE_INT, {.i64=128}, 0, 256, FLAGS },
  1215. { "lfe_high", "LFE high cut off", OFFSET(highcutf), AV_OPT_TYPE_INT, {.i64=256}, 0, 512, FLAGS },
  1216. { "lfe_mode", "set LFE channel mode", OFFSET(lfe_mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, TFLAGS, "lfe_mode" },
  1217. { "add", "just add LFE channel", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 1, TFLAGS, "lfe_mode" },
  1218. { "sub", "subtract LFE channel with others", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 1, TFLAGS, "lfe_mode" },
  1219. { "smooth", "set temporal smoothness strength", OFFSET(smooth), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, TFLAGS },
  1220. { "angle", "set soundfield transform angle", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl=90}, 0, 360, TFLAGS },
  1221. { "focus", "set soundfield transform focus", OFFSET(focus), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, TFLAGS },
  1222. { "fc_in", "set front center channel input level", OFFSET(f_i[SC_FC]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1223. { "fc_out", "set front center channel output level", OFFSET(f_o[SC_FC]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1224. { "fl_in", "set front left channel input level", OFFSET(f_i[SC_FL]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1225. { "fl_out", "set front left channel output level", OFFSET(f_o[SC_FL]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1226. { "fr_in", "set front right channel input level", OFFSET(f_i[SC_FR]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1227. { "fr_out", "set front right channel output level", OFFSET(f_o[SC_FR]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1228. { "sl_in", "set side left channel input level", OFFSET(f_i[SC_SL]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1229. { "sl_out", "set side left channel output level", OFFSET(f_o[SC_SL]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1230. { "sr_in", "set side right channel input level", OFFSET(f_i[SC_SR]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1231. { "sr_out", "set side right channel output level", OFFSET(f_o[SC_SR]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1232. { "bl_in", "set back left channel input level", OFFSET(f_i[SC_BL]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1233. { "bl_out", "set back left channel output level", OFFSET(f_o[SC_BL]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1234. { "br_in", "set back right channel input level", OFFSET(f_i[SC_BR]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1235. { "br_out", "set back right channel output level", OFFSET(f_o[SC_BR]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1236. { "bc_in", "set back center channel input level", OFFSET(f_i[SC_BC]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1237. { "bc_out", "set back center channel output level", OFFSET(f_o[SC_BC]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1238. { "lfe_in", "set lfe channel input level", OFFSET(f_i[SC_LF]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1239. { "lfe_out", "set lfe channel output level", OFFSET(f_o[SC_LF]), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, TFLAGS },
  1240. { "allx", "set all channel's x spread", OFFSET(all_x), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 15, TFLAGS },
  1241. { "ally", "set all channel's y spread", OFFSET(all_y), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 15, TFLAGS },
  1242. { "fcx", "set front center channel x spread", OFFSET(f_x[SC_FC]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1243. { "flx", "set front left channel x spread", OFFSET(f_x[SC_FL]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1244. { "frx", "set front right channel x spread", OFFSET(f_x[SC_FR]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1245. { "blx", "set back left channel x spread", OFFSET(f_x[SC_BL]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1246. { "brx", "set back right channel x spread", OFFSET(f_x[SC_BR]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1247. { "slx", "set side left channel x spread", OFFSET(f_x[SC_SL]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1248. { "srx", "set side right channel x spread", OFFSET(f_x[SC_SR]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1249. { "bcx", "set back center channel x spread", OFFSET(f_x[SC_BC]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1250. { "fcy", "set front center channel y spread", OFFSET(f_y[SC_FC]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1251. { "fly", "set front left channel y spread", OFFSET(f_y[SC_FL]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1252. { "fry", "set front right channel y spread", OFFSET(f_y[SC_FR]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1253. { "bly", "set back left channel y spread", OFFSET(f_y[SC_BL]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1254. { "bry", "set back right channel y spread", OFFSET(f_y[SC_BR]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1255. { "sly", "set side left channel y spread", OFFSET(f_y[SC_SL]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1256. { "sry", "set side right channel y spread", OFFSET(f_y[SC_SR]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1257. { "bcy", "set back center channel y spread", OFFSET(f_y[SC_BC]), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, .06, 15, TFLAGS },
  1258. { "win_size", "set window size", OFFSET(win_size), AV_OPT_TYPE_INT, {.i64=4096},1024,65536,FLAGS },
  1259. WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_HANNING),
  1260. { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, TFLAGS },
  1261. { NULL }
  1262. };
  1263. AVFILTER_DEFINE_CLASS(surround);
  1264. static const AVFilterPad inputs[] = {
  1265. {
  1266. .name = "default",
  1267. .type = AVMEDIA_TYPE_AUDIO,
  1268. .config_props = config_input,
  1269. },
  1270. };
  1271. static const AVFilterPad outputs[] = {
  1272. {
  1273. .name = "default",
  1274. .type = AVMEDIA_TYPE_AUDIO,
  1275. .config_props = config_output,
  1276. },
  1277. };
  1278. const AVFilter ff_af_surround = {
  1279. .name = "surround",
  1280. .description = NULL_IF_CONFIG_SMALL("Apply audio surround upmix filter."),
  1281. .priv_size = sizeof(AudioSurroundContext),
  1282. .priv_class = &surround_class,
  1283. .init = init,
  1284. .uninit = uninit,
  1285. .activate = activate,
  1286. FILTER_INPUTS(inputs),
  1287. FILTER_OUTPUTS(outputs),
  1288. FILTER_QUERY_FUNC(query_formats),
  1289. .flags = AVFILTER_FLAG_SLICE_THREADS,
  1290. .process_command = process_command,
  1291. };