af_surround.c 60 KB

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