You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

742 lines
21KB

  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. /**
  21. * @file
  22. * An arbitrary audio FIR filter
  23. */
  24. #include <float.h>
  25. #include "libavutil/audio_fifo.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/float_dsp.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/xga_font_data.h"
  31. #include "libavcodec/avfft.h"
  32. #include "audio.h"
  33. #include "avfilter.h"
  34. #include "formats.h"
  35. #include "internal.h"
  36. #include "af_afir.h"
  37. static void fcmul_add_c(float *sum, const float *t, const float *c, ptrdiff_t len)
  38. {
  39. int n;
  40. for (n = 0; n < len; n++) {
  41. const float cre = c[2 * n ];
  42. const float cim = c[2 * n + 1];
  43. const float tre = t[2 * n ];
  44. const float tim = t[2 * n + 1];
  45. sum[2 * n ] += tre * cre - tim * cim;
  46. sum[2 * n + 1] += tre * cim + tim * cre;
  47. }
  48. sum[2 * n] += t[2 * n] * c[2 * n];
  49. }
  50. static int fir_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
  51. {
  52. AudioFIRContext *s = ctx->priv;
  53. const float *src = (const float *)s->in[0]->extended_data[ch];
  54. int index1 = (s->index + 1) % 3;
  55. int index2 = (s->index + 2) % 3;
  56. float *sum = s->sum[ch];
  57. AVFrame *out = arg;
  58. float *block;
  59. float *dst;
  60. int n, i, j;
  61. memset(sum, 0, sizeof(*sum) * s->fft_length);
  62. block = s->block[ch] + s->part_index * s->block_size;
  63. memset(block, 0, sizeof(*block) * s->fft_length);
  64. s->fdsp->vector_fmul_scalar(block + s->part_size, src, s->dry_gain, FFALIGN(s->nb_samples, 4));
  65. emms_c();
  66. av_rdft_calc(s->rdft[ch], block);
  67. block[2 * s->part_size] = block[1];
  68. block[1] = 0;
  69. j = s->part_index;
  70. for (i = 0; i < s->nb_partitions; i++) {
  71. const int coffset = i * s->coeff_size;
  72. const FFTComplex *coeff = s->coeff[ch * !s->one2many] + coffset;
  73. block = s->block[ch] + j * s->block_size;
  74. s->fcmul_add(sum, block, (const float *)coeff, s->part_size);
  75. if (j == 0)
  76. j = s->nb_partitions;
  77. j--;
  78. }
  79. sum[1] = sum[2 * s->part_size];
  80. av_rdft_calc(s->irdft[ch], sum);
  81. dst = (float *)s->buffer->extended_data[ch] + index1 * s->part_size;
  82. for (n = 0; n < s->part_size; n++) {
  83. dst[n] += sum[n];
  84. }
  85. dst = (float *)s->buffer->extended_data[ch] + index2 * s->part_size;
  86. memcpy(dst, sum + s->part_size, s->part_size * sizeof(*dst));
  87. dst = (float *)s->buffer->extended_data[ch] + s->index * s->part_size;
  88. if (out) {
  89. float *ptr = (float *)out->extended_data[ch];
  90. s->fdsp->vector_fmul_scalar(ptr, dst, s->wet_gain, FFALIGN(out->nb_samples, 4));
  91. emms_c();
  92. }
  93. return 0;
  94. }
  95. static int fir_frame(AudioFIRContext *s, AVFilterLink *outlink)
  96. {
  97. AVFilterContext *ctx = outlink->src;
  98. AVFrame *out = NULL;
  99. int ret;
  100. s->nb_samples = FFMIN(s->part_size, av_audio_fifo_size(s->fifo[0]));
  101. if (!s->want_skip) {
  102. out = ff_get_audio_buffer(outlink, s->nb_samples);
  103. if (!out)
  104. return AVERROR(ENOMEM);
  105. }
  106. s->in[0] = ff_get_audio_buffer(ctx->inputs[0], s->nb_samples);
  107. if (!s->in[0]) {
  108. av_frame_free(&out);
  109. return AVERROR(ENOMEM);
  110. }
  111. av_audio_fifo_peek(s->fifo[0], (void **)s->in[0]->extended_data, s->nb_samples);
  112. ctx->internal->execute(ctx, fir_channel, out, NULL, outlink->channels);
  113. s->part_index = (s->part_index + 1) % s->nb_partitions;
  114. av_audio_fifo_drain(s->fifo[0], s->nb_samples);
  115. if (!s->want_skip) {
  116. out->pts = s->pts;
  117. if (s->pts != AV_NOPTS_VALUE)
  118. s->pts += av_rescale_q(out->nb_samples, (AVRational){1, outlink->sample_rate}, outlink->time_base);
  119. }
  120. s->index++;
  121. if (s->index == 3)
  122. s->index = 0;
  123. av_frame_free(&s->in[0]);
  124. if (s->want_skip == 1) {
  125. s->want_skip = 0;
  126. ret = 0;
  127. } else {
  128. ret = ff_filter_frame(outlink, out);
  129. }
  130. return ret;
  131. }
  132. static void drawtext(AVFrame *pic, int x, int y, const char *txt, uint32_t color)
  133. {
  134. const uint8_t *font;
  135. int font_height;
  136. int i;
  137. font = avpriv_cga_font, font_height = 8;
  138. for (i = 0; txt[i]; i++) {
  139. int char_y, mask;
  140. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  141. for (char_y = 0; char_y < font_height; char_y++) {
  142. for (mask = 0x80; mask; mask >>= 1) {
  143. if (font[txt[i] * font_height + char_y] & mask)
  144. AV_WL32(p, color);
  145. p += 4;
  146. }
  147. p += pic->linesize[0] - 8 * 4;
  148. }
  149. }
  150. }
  151. static void draw_line(AVFrame *out, int x0, int y0, int x1, int y1, uint32_t color)
  152. {
  153. int dx = FFABS(x1-x0);
  154. int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
  155. int err = (dx>dy ? dx : -dy) / 2, e2;
  156. for (;;) {
  157. AV_WL32(out->data[0] + y0 * out->linesize[0] + x0 * 4, color);
  158. if (x0 == x1 && y0 == y1)
  159. break;
  160. e2 = err;
  161. if (e2 >-dx) {
  162. err -= dy;
  163. x0--;
  164. }
  165. if (e2 < dy) {
  166. err += dx;
  167. y0 += sy;
  168. }
  169. }
  170. }
  171. static void draw_response(AVFilterContext *ctx, AVFrame *out)
  172. {
  173. AudioFIRContext *s = ctx->priv;
  174. float *mag, *phase, min = FLT_MAX, max = FLT_MIN;
  175. int prev_ymag = -1, prev_yphase = -1;
  176. char text[32];
  177. int channel, i, x;
  178. memset(out->data[0], 0, s->h * out->linesize[0]);
  179. phase = av_malloc_array(s->w, sizeof(*phase));
  180. mag = av_malloc_array(s->w, sizeof(*mag));
  181. if (!mag || !phase)
  182. goto end;
  183. channel = av_clip(s->ir_channel, 0, s->in[1]->channels - 1);
  184. for (i = 0; i < s->w; i++) {
  185. const float *src = (const float *)s->in[1]->extended_data[channel];
  186. double w = i * M_PI / (s->w - 1);
  187. double real = 0.;
  188. double imag = 0.;
  189. for (x = 0; x < s->nb_taps; x++) {
  190. real += cos(-x * w) * src[x];
  191. imag += sin(-x * w) * src[x];
  192. }
  193. mag[i] = hypot(real, imag);
  194. phase[i] = atan2(imag, real);
  195. min = fminf(min, mag[i]);
  196. max = fmaxf(max, mag[i]);
  197. }
  198. for (i = 0; i < s->w; i++) {
  199. int ymag = mag[i] / max * (s->h - 1);
  200. int yphase = (0.5 * (1. + phase[i] / M_PI)) * (s->h - 1);
  201. ymag = s->h - 1 - av_clip(ymag, 0, s->h - 1);
  202. yphase = s->h - 1 - av_clip(yphase, 0, s->h - 1);
  203. if (prev_ymag < 0)
  204. prev_ymag = ymag;
  205. if (prev_yphase < 0)
  206. prev_yphase = yphase;
  207. draw_line(out, i, ymag, FFMAX(i - 1, 0), prev_ymag, 0xFFFF00FF);
  208. draw_line(out, i, yphase, FFMAX(i - 1, 0), prev_yphase, 0xFF00FF00);
  209. prev_ymag = ymag;
  210. prev_yphase = yphase;
  211. }
  212. if (s->w > 400 && s->h > 100) {
  213. drawtext(out, 2, 2, "Max Magnitude:", 0xDDDDDDDD);
  214. snprintf(text, sizeof(text), "%.2f", max);
  215. drawtext(out, 15 * 8 + 2, 2, text, 0xDDDDDDDD);
  216. drawtext(out, 2, 12, "Min Magnitude:", 0xDDDDDDDD);
  217. snprintf(text, sizeof(text), "%.2f", min);
  218. drawtext(out, 15 * 8 + 2, 12, text, 0xDDDDDDDD);
  219. }
  220. end:
  221. av_free(phase);
  222. av_free(mag);
  223. }
  224. static int convert_coeffs(AVFilterContext *ctx)
  225. {
  226. AudioFIRContext *s = ctx->priv;
  227. int i, ch, n, N;
  228. s->nb_taps = av_audio_fifo_size(s->fifo[1]);
  229. if (s->nb_taps <= 0)
  230. return AVERROR(EINVAL);
  231. for (n = 4; (1 << n) < s->nb_taps; n++);
  232. N = FFMIN(n, 16);
  233. s->ir_length = 1 << n;
  234. s->fft_length = (1 << (N + 1)) + 1;
  235. s->part_size = 1 << (N - 1);
  236. s->block_size = FFALIGN(s->fft_length, 32);
  237. s->coeff_size = FFALIGN(s->part_size + 1, 32);
  238. s->nb_partitions = (s->nb_taps + s->part_size - 1) / s->part_size;
  239. s->nb_coeffs = s->ir_length + s->nb_partitions;
  240. for (ch = 0; ch < ctx->inputs[0]->channels; ch++) {
  241. s->sum[ch] = av_calloc(s->fft_length, sizeof(**s->sum));
  242. if (!s->sum[ch])
  243. return AVERROR(ENOMEM);
  244. }
  245. for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
  246. s->coeff[ch] = av_calloc(s->nb_partitions * s->coeff_size, sizeof(**s->coeff));
  247. if (!s->coeff[ch])
  248. return AVERROR(ENOMEM);
  249. }
  250. for (ch = 0; ch < ctx->inputs[0]->channels; ch++) {
  251. s->block[ch] = av_calloc(s->nb_partitions * s->block_size, sizeof(**s->block));
  252. if (!s->block[ch])
  253. return AVERROR(ENOMEM);
  254. }
  255. for (ch = 0; ch < ctx->inputs[0]->channels; ch++) {
  256. s->rdft[ch] = av_rdft_init(N, DFT_R2C);
  257. s->irdft[ch] = av_rdft_init(N, IDFT_C2R);
  258. if (!s->rdft[ch] || !s->irdft[ch])
  259. return AVERROR(ENOMEM);
  260. }
  261. s->in[1] = ff_get_audio_buffer(ctx->inputs[1], s->nb_taps);
  262. if (!s->in[1])
  263. return AVERROR(ENOMEM);
  264. s->buffer = ff_get_audio_buffer(ctx->inputs[0], s->part_size * 3);
  265. if (!s->buffer)
  266. return AVERROR(ENOMEM);
  267. av_audio_fifo_read(s->fifo[1], (void **)s->in[1]->extended_data, s->nb_taps);
  268. if (s->response)
  269. draw_response(ctx, s->video);
  270. if (s->again) {
  271. float power = 0;
  272. for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
  273. float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
  274. for (i = 0; i < s->nb_taps; i++)
  275. power += FFABS(time[i]);
  276. }
  277. s->gain = sqrtf(1.f / (ctx->inputs[1]->channels * power)) / (sqrtf(ctx->inputs[1]->channels));
  278. for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
  279. float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
  280. s->fdsp->vector_fmul_scalar(time, time, s->gain, FFALIGN(s->nb_taps, 4));
  281. }
  282. }
  283. for (ch = 0; ch < ctx->inputs[1]->channels; ch++) {
  284. float *time = (float *)s->in[1]->extended_data[!s->one2many * ch];
  285. float *block = s->block[ch];
  286. FFTComplex *coeff = s->coeff[ch];
  287. for (i = FFMAX(1, s->length * s->nb_taps); i < s->nb_taps; i++)
  288. time[i] = 0;
  289. for (i = 0; i < s->nb_partitions; i++) {
  290. const float scale = 1.f / s->part_size;
  291. const int toffset = i * s->part_size;
  292. const int coffset = i * s->coeff_size;
  293. const int boffset = s->part_size;
  294. const int remaining = s->nb_taps - (i * s->part_size);
  295. const int size = remaining >= s->part_size ? s->part_size : remaining;
  296. memset(block, 0, sizeof(*block) * s->fft_length);
  297. memcpy(block + boffset, time + toffset, size * sizeof(*block));
  298. av_rdft_calc(s->rdft[0], block);
  299. coeff[coffset].re = block[0] * scale;
  300. coeff[coffset].im = 0;
  301. for (n = 1; n < s->part_size; n++) {
  302. coeff[coffset + n].re = block[2 * n] * scale;
  303. coeff[coffset + n].im = block[2 * n + 1] * scale;
  304. }
  305. coeff[coffset + s->part_size].re = block[1] * scale;
  306. coeff[coffset + s->part_size].im = 0;
  307. }
  308. }
  309. av_frame_free(&s->in[1]);
  310. av_log(ctx, AV_LOG_DEBUG, "nb_taps: %d\n", s->nb_taps);
  311. av_log(ctx, AV_LOG_DEBUG, "nb_partitions: %d\n", s->nb_partitions);
  312. av_log(ctx, AV_LOG_DEBUG, "partition size: %d\n", s->part_size);
  313. av_log(ctx, AV_LOG_DEBUG, "ir_length: %d\n", s->ir_length);
  314. s->have_coeffs = 1;
  315. return 0;
  316. }
  317. static int read_ir(AVFilterLink *link, AVFrame *frame)
  318. {
  319. AVFilterContext *ctx = link->dst;
  320. AudioFIRContext *s = ctx->priv;
  321. int nb_taps, max_nb_taps, ret;
  322. ret = av_audio_fifo_write(s->fifo[1], (void **)frame->extended_data,
  323. frame->nb_samples);
  324. av_frame_free(&frame);
  325. if (ret < 0)
  326. return ret;
  327. nb_taps = av_audio_fifo_size(s->fifo[1]);
  328. max_nb_taps = s->max_ir_len * ctx->outputs[0]->sample_rate;
  329. if (nb_taps > max_nb_taps) {
  330. av_log(ctx, AV_LOG_ERROR, "Too big number of coefficients: %d > %d.\n", nb_taps, max_nb_taps);
  331. return AVERROR(EINVAL);
  332. }
  333. return 0;
  334. }
  335. static int filter_frame(AVFilterLink *link, AVFrame *frame)
  336. {
  337. AVFilterContext *ctx = link->dst;
  338. AudioFIRContext *s = ctx->priv;
  339. AVFilterLink *outlink = ctx->outputs[0];
  340. int ret;
  341. ret = av_audio_fifo_write(s->fifo[0], (void **)frame->extended_data,
  342. frame->nb_samples);
  343. if (ret > 0 && s->pts == AV_NOPTS_VALUE)
  344. s->pts = frame->pts;
  345. av_frame_free(&frame);
  346. if (ret < 0)
  347. return ret;
  348. if (!s->have_coeffs && s->eof_coeffs) {
  349. ret = convert_coeffs(ctx);
  350. if (ret < 0)
  351. return ret;
  352. }
  353. if (s->response && s->have_coeffs) {
  354. s->video->pts = s->pts;
  355. ret = ff_filter_frame(ctx->outputs[1], av_frame_clone(s->video));
  356. if (ret < 0)
  357. return ret;
  358. }
  359. if (s->have_coeffs) {
  360. while (av_audio_fifo_size(s->fifo[0]) >= s->part_size) {
  361. ret = fir_frame(s, outlink);
  362. if (ret < 0)
  363. return ret;
  364. }
  365. }
  366. return 0;
  367. }
  368. static int request_frame(AVFilterLink *outlink)
  369. {
  370. AVFilterContext *ctx = outlink->src;
  371. AudioFIRContext *s = ctx->priv;
  372. int ret;
  373. if (!s->eof_coeffs) {
  374. ret = ff_request_frame(ctx->inputs[1]);
  375. if (ret == AVERROR_EOF) {
  376. s->eof_coeffs = 1;
  377. ret = 0;
  378. }
  379. return ret;
  380. }
  381. ret = ff_request_frame(ctx->inputs[0]);
  382. if (ret == AVERROR_EOF && s->have_coeffs) {
  383. if (s->need_padding) {
  384. AVFrame *silence = ff_get_audio_buffer(outlink, s->part_size);
  385. if (!silence)
  386. return AVERROR(ENOMEM);
  387. ret = av_audio_fifo_write(s->fifo[0], (void **)silence->extended_data,
  388. silence->nb_samples);
  389. av_frame_free(&silence);
  390. if (ret < 0)
  391. return ret;
  392. s->need_padding = 0;
  393. }
  394. while (av_audio_fifo_size(s->fifo[0]) > 0) {
  395. ret = fir_frame(s, outlink);
  396. if (ret < 0)
  397. return ret;
  398. }
  399. ret = AVERROR_EOF;
  400. }
  401. return ret;
  402. }
  403. static int query_formats(AVFilterContext *ctx)
  404. {
  405. AudioFIRContext *s = ctx->priv;
  406. AVFilterFormats *formats;
  407. AVFilterChannelLayouts *layouts;
  408. static const enum AVSampleFormat sample_fmts[] = {
  409. AV_SAMPLE_FMT_FLTP,
  410. AV_SAMPLE_FMT_NONE
  411. };
  412. static const enum AVPixelFormat pix_fmts[] = {
  413. AV_PIX_FMT_RGB0,
  414. AV_PIX_FMT_NONE
  415. };
  416. int ret, i;
  417. if (s->response) {
  418. AVFilterLink *videolink = ctx->outputs[1];
  419. formats = ff_make_format_list(pix_fmts);
  420. if ((ret = ff_formats_ref(formats, &videolink->in_formats)) < 0)
  421. return ret;
  422. }
  423. layouts = ff_all_channel_counts();
  424. if ((ret = ff_channel_layouts_ref(layouts, &ctx->outputs[0]->in_channel_layouts)) < 0)
  425. return ret;
  426. for (i = 0; i < 2; i++) {
  427. layouts = ff_all_channel_counts();
  428. if ((ret = ff_channel_layouts_ref(layouts, &ctx->inputs[i]->out_channel_layouts)) < 0)
  429. return ret;
  430. }
  431. formats = ff_make_format_list(sample_fmts);
  432. if ((ret = ff_set_common_formats(ctx, formats)) < 0)
  433. return ret;
  434. formats = ff_all_samplerates();
  435. return ff_set_common_samplerates(ctx, formats);
  436. }
  437. static int config_output(AVFilterLink *outlink)
  438. {
  439. AVFilterContext *ctx = outlink->src;
  440. AudioFIRContext *s = ctx->priv;
  441. if (ctx->inputs[0]->channels != ctx->inputs[1]->channels &&
  442. ctx->inputs[1]->channels != 1) {
  443. av_log(ctx, AV_LOG_ERROR,
  444. "Second input must have same number of channels as first input or "
  445. "exactly 1 channel.\n");
  446. return AVERROR(EINVAL);
  447. }
  448. s->one2many = ctx->inputs[1]->channels == 1;
  449. outlink->sample_rate = ctx->inputs[0]->sample_rate;
  450. outlink->time_base = ctx->inputs[0]->time_base;
  451. outlink->channel_layout = ctx->inputs[0]->channel_layout;
  452. outlink->channels = ctx->inputs[0]->channels;
  453. s->fifo[0] = av_audio_fifo_alloc(ctx->inputs[0]->format, ctx->inputs[0]->channels, 1024);
  454. s->fifo[1] = av_audio_fifo_alloc(ctx->inputs[1]->format, ctx->inputs[1]->channels, 1024);
  455. if (!s->fifo[0] || !s->fifo[1])
  456. return AVERROR(ENOMEM);
  457. s->sum = av_calloc(outlink->channels, sizeof(*s->sum));
  458. s->coeff = av_calloc(ctx->inputs[1]->channels, sizeof(*s->coeff));
  459. s->block = av_calloc(ctx->inputs[0]->channels, sizeof(*s->block));
  460. s->rdft = av_calloc(outlink->channels, sizeof(*s->rdft));
  461. s->irdft = av_calloc(outlink->channels, sizeof(*s->irdft));
  462. if (!s->sum || !s->coeff || !s->block || !s->rdft || !s->irdft)
  463. return AVERROR(ENOMEM);
  464. s->nb_channels = outlink->channels;
  465. s->nb_coef_channels = ctx->inputs[1]->channels;
  466. s->want_skip = 1;
  467. s->need_padding = 1;
  468. s->pts = AV_NOPTS_VALUE;
  469. return 0;
  470. }
  471. static av_cold void uninit(AVFilterContext *ctx)
  472. {
  473. AudioFIRContext *s = ctx->priv;
  474. int ch;
  475. if (s->sum) {
  476. for (ch = 0; ch < s->nb_channels; ch++) {
  477. av_freep(&s->sum[ch]);
  478. }
  479. }
  480. av_freep(&s->sum);
  481. if (s->coeff) {
  482. for (ch = 0; ch < s->nb_coef_channels; ch++) {
  483. av_freep(&s->coeff[ch]);
  484. }
  485. }
  486. av_freep(&s->coeff);
  487. if (s->block) {
  488. for (ch = 0; ch < s->nb_channels; ch++) {
  489. av_freep(&s->block[ch]);
  490. }
  491. }
  492. av_freep(&s->block);
  493. if (s->rdft) {
  494. for (ch = 0; ch < s->nb_channels; ch++) {
  495. av_rdft_end(s->rdft[ch]);
  496. }
  497. }
  498. av_freep(&s->rdft);
  499. if (s->irdft) {
  500. for (ch = 0; ch < s->nb_channels; ch++) {
  501. av_rdft_end(s->irdft[ch]);
  502. }
  503. }
  504. av_freep(&s->irdft);
  505. av_frame_free(&s->in[0]);
  506. av_frame_free(&s->in[1]);
  507. av_frame_free(&s->buffer);
  508. av_audio_fifo_free(s->fifo[0]);
  509. av_audio_fifo_free(s->fifo[1]);
  510. av_freep(&s->fdsp);
  511. av_freep(&ctx->output_pads[0].name);
  512. if (s->response)
  513. av_freep(&ctx->output_pads[1].name);
  514. av_frame_free(&s->video);
  515. }
  516. static int config_video(AVFilterLink *outlink)
  517. {
  518. AVFilterContext *ctx = outlink->src;
  519. AudioFIRContext *s = ctx->priv;
  520. outlink->sample_aspect_ratio = (AVRational){1,1};
  521. outlink->w = s->w;
  522. outlink->h = s->h;
  523. av_frame_free(&s->video);
  524. s->video = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  525. if (!s->video)
  526. return AVERROR(ENOMEM);
  527. return 0;
  528. }
  529. static av_cold int init(AVFilterContext *ctx)
  530. {
  531. AudioFIRContext *s = ctx->priv;
  532. AVFilterPad pad, vpad;
  533. pad = (AVFilterPad){
  534. .name = av_strdup("default"),
  535. .type = AVMEDIA_TYPE_AUDIO,
  536. .config_props = config_output,
  537. .request_frame = request_frame,
  538. };
  539. if (!pad.name)
  540. return AVERROR(ENOMEM);
  541. if (s->response) {
  542. vpad = (AVFilterPad){
  543. .name = av_strdup("filter_response"),
  544. .type = AVMEDIA_TYPE_VIDEO,
  545. .config_props = config_video,
  546. };
  547. if (!vpad.name)
  548. return AVERROR(ENOMEM);
  549. }
  550. ff_insert_outpad(ctx, 0, &pad);
  551. if (s->response)
  552. ff_insert_outpad(ctx, 1, &vpad);
  553. s->fcmul_add = fcmul_add_c;
  554. s->fdsp = avpriv_float_dsp_alloc(0);
  555. if (!s->fdsp)
  556. return AVERROR(ENOMEM);
  557. if (ARCH_X86)
  558. ff_afir_init_x86(s);
  559. return 0;
  560. }
  561. static const AVFilterPad afir_inputs[] = {
  562. {
  563. .name = "main",
  564. .type = AVMEDIA_TYPE_AUDIO,
  565. .filter_frame = filter_frame,
  566. },{
  567. .name = "ir",
  568. .type = AVMEDIA_TYPE_AUDIO,
  569. .filter_frame = read_ir,
  570. },
  571. { NULL }
  572. };
  573. #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  574. #define VF AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  575. #define OFFSET(x) offsetof(AudioFIRContext, x)
  576. static const AVOption afir_options[] = {
  577. { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, AF },
  578. { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 10, AF },
  579. { "length", "set IR length", OFFSET(length), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AF },
  580. { "again", "enable auto gain", OFFSET(again), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, AF },
  581. { "maxir", "set max IR length", OFFSET(max_ir_len), AV_OPT_TYPE_FLOAT, {.dbl=30}, 0.1, 60, AF },
  582. { "response", "show IR frequency response", OFFSET(response), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
  583. { "channel", "set IR channel to display frequency response", OFFSET(ir_channel), AV_OPT_TYPE_INT, {.i64=0}, 0, 1024, VF },
  584. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"}, 0, 0, VF },
  585. { NULL }
  586. };
  587. AVFILTER_DEFINE_CLASS(afir);
  588. AVFilter ff_af_afir = {
  589. .name = "afir",
  590. .description = NULL_IF_CONFIG_SMALL("Apply Finite Impulse Response filter with supplied coefficients in 2nd stream."),
  591. .priv_size = sizeof(AudioFIRContext),
  592. .priv_class = &afir_class,
  593. .query_formats = query_formats,
  594. .init = init,
  595. .uninit = uninit,
  596. .inputs = afir_inputs,
  597. .flags = AVFILTER_FLAG_DYNAMIC_OUTPUTS |
  598. AVFILTER_FLAG_SLICE_THREADS,
  599. };