af_aderivative.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "audio.h"
  19. #include "avfilter.h"
  20. #include "internal.h"
  21. typedef struct ADerivativeContext {
  22. const AVClass *class;
  23. AVFrame *prev;
  24. void (*filter)(void **dst, void **prv, const void **src,
  25. int nb_samples, int channels);
  26. } ADerivativeContext;
  27. static int query_formats(AVFilterContext *ctx)
  28. {
  29. AVFilterFormats *formats = NULL;
  30. AVFilterChannelLayouts *layouts = NULL;
  31. static const enum AVSampleFormat derivative_sample_fmts[] = {
  32. AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_FLTP,
  33. AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_DBLP,
  34. AV_SAMPLE_FMT_NONE
  35. };
  36. static const enum AVSampleFormat integral_sample_fmts[] = {
  37. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP,
  38. AV_SAMPLE_FMT_NONE
  39. };
  40. int ret;
  41. formats = ff_make_format_list(strcmp(ctx->filter->name, "aintegral") ?
  42. derivative_sample_fmts : integral_sample_fmts);
  43. if (!formats)
  44. return AVERROR(ENOMEM);
  45. ret = ff_set_common_formats(ctx, formats);
  46. if (ret < 0)
  47. return ret;
  48. layouts = ff_all_channel_counts();
  49. if (!layouts)
  50. return AVERROR(ENOMEM);
  51. ret = ff_set_common_channel_layouts(ctx, layouts);
  52. if (ret < 0)
  53. return ret;
  54. formats = ff_all_samplerates();
  55. return ff_set_common_samplerates(ctx, formats);
  56. }
  57. #define DERIVATIVE(name, type) \
  58. static void aderivative_## name ##p(void **d, void **p, const void **s, \
  59. int nb_samples, int channels) \
  60. { \
  61. int n, c; \
  62. \
  63. for (c = 0; c < channels; c++) { \
  64. const type *src = s[c]; \
  65. type *dst = d[c]; \
  66. type *prv = p[c]; \
  67. \
  68. for (n = 0; n < nb_samples; n++) { \
  69. const type current = src[n]; \
  70. \
  71. dst[n] = current - prv[0]; \
  72. prv[0] = current; \
  73. } \
  74. } \
  75. }
  76. DERIVATIVE(flt, float)
  77. DERIVATIVE(dbl, double)
  78. DERIVATIVE(s16, int16_t)
  79. DERIVATIVE(s32, int32_t)
  80. #define INTEGRAL(name, type) \
  81. static void aintegral_## name ##p(void **d, void **p, const void **s, \
  82. int nb_samples, int channels) \
  83. { \
  84. int n, c; \
  85. \
  86. for (c = 0; c < channels; c++) { \
  87. const type *src = s[c]; \
  88. type *dst = d[c]; \
  89. type *prv = p[c]; \
  90. \
  91. for (n = 0; n < nb_samples; n++) { \
  92. const type current = src[n]; \
  93. \
  94. dst[n] = current + prv[0]; \
  95. prv[0] = dst[n]; \
  96. } \
  97. } \
  98. }
  99. INTEGRAL(flt, float)
  100. INTEGRAL(dbl, double)
  101. static int config_input(AVFilterLink *inlink)
  102. {
  103. AVFilterContext *ctx = inlink->dst;
  104. ADerivativeContext *s = ctx->priv;
  105. switch (inlink->format) {
  106. case AV_SAMPLE_FMT_FLTP: s->filter = aderivative_fltp; break;
  107. case AV_SAMPLE_FMT_DBLP: s->filter = aderivative_dblp; break;
  108. case AV_SAMPLE_FMT_S32P: s->filter = aderivative_s32p; break;
  109. case AV_SAMPLE_FMT_S16P: s->filter = aderivative_s16p; break;
  110. }
  111. if (strcmp(ctx->filter->name, "aintegral"))
  112. return 0;
  113. switch (inlink->format) {
  114. case AV_SAMPLE_FMT_FLTP: s->filter = aintegral_fltp; break;
  115. case AV_SAMPLE_FMT_DBLP: s->filter = aintegral_dblp; break;
  116. }
  117. return 0;
  118. }
  119. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. ADerivativeContext *s = ctx->priv;
  123. AVFilterLink *outlink = ctx->outputs[0];
  124. AVFrame *out = ff_get_audio_buffer(outlink, in->nb_samples);
  125. if (!out) {
  126. av_frame_free(&in);
  127. return AVERROR(ENOMEM);
  128. }
  129. av_frame_copy_props(out, in);
  130. if (!s->prev) {
  131. s->prev = ff_get_audio_buffer(inlink, 1);
  132. if (!s->prev) {
  133. av_frame_free(&in);
  134. return AVERROR(ENOMEM);
  135. }
  136. }
  137. s->filter((void **)out->extended_data, (void **)s->prev->extended_data, (const void **)in->extended_data,
  138. in->nb_samples, in->channels);
  139. av_frame_free(&in);
  140. return ff_filter_frame(outlink, out);
  141. }
  142. static av_cold void uninit(AVFilterContext *ctx)
  143. {
  144. ADerivativeContext *s = ctx->priv;
  145. av_frame_free(&s->prev);
  146. }
  147. static const AVFilterPad aderivative_inputs[] = {
  148. {
  149. .name = "default",
  150. .type = AVMEDIA_TYPE_AUDIO,
  151. .filter_frame = filter_frame,
  152. .config_props = config_input,
  153. },
  154. { NULL }
  155. };
  156. static const AVFilterPad aderivative_outputs[] = {
  157. {
  158. .name = "default",
  159. .type = AVMEDIA_TYPE_AUDIO,
  160. },
  161. { NULL }
  162. };
  163. AVFilter ff_af_aderivative = {
  164. .name = "aderivative",
  165. .description = NULL_IF_CONFIG_SMALL("Compute derivative of input audio."),
  166. .query_formats = query_formats,
  167. .priv_size = sizeof(ADerivativeContext),
  168. .uninit = uninit,
  169. .inputs = aderivative_inputs,
  170. .outputs = aderivative_outputs,
  171. };
  172. AVFilter ff_af_aintegral = {
  173. .name = "aintegral",
  174. .description = NULL_IF_CONFIG_SMALL("Compute integral of input audio."),
  175. .query_formats = query_formats,
  176. .priv_size = sizeof(ADerivativeContext),
  177. .uninit = uninit,
  178. .inputs = aderivative_inputs,
  179. .outputs = aderivative_outputs,
  180. };