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.

259 lines
9.1KB

  1. /*
  2. * Copyright (c) 2015 Kyle Swanson <k@ylo.ph>.
  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 License
  8. * 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
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "audio.h"
  22. #include "avfilter.h"
  23. #include "filters.h"
  24. #include "internal.h"
  25. #include "libavutil/lfg.h"
  26. #include "libavutil/random_seed.h"
  27. typedef struct ANoiseSrcContext {
  28. const AVClass *class;
  29. int sample_rate;
  30. double amplitude;
  31. int64_t duration;
  32. int color;
  33. int64_t seed;
  34. int nb_samples;
  35. int64_t pts;
  36. int infinite;
  37. double (*filter)(double white, double *buf, double half_amplitude);
  38. double buf[7];
  39. AVLFG c;
  40. } ANoiseSrcContext;
  41. enum NoiseMode {
  42. NM_WHITE,
  43. NM_PINK,
  44. NM_BROWN,
  45. NM_BLUE,
  46. NM_VIOLET,
  47. NM_VELVET,
  48. NM_NB
  49. };
  50. #define OFFSET(x) offsetof(ANoiseSrcContext, x)
  51. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  52. static const AVOption anoisesrc_options[] = {
  53. { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
  54. { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 15, INT_MAX, FLAGS },
  55. { "amplitude", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
  56. { "a", "set amplitude", OFFSET(amplitude), AV_OPT_TYPE_DOUBLE, {.dbl = 1.}, 0., 1., FLAGS },
  57. { "duration", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
  58. { "d", "set duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, INT64_MAX, FLAGS },
  59. { "color", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" },
  60. { "colour", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" },
  61. { "c", "set noise color", OFFSET(color), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NM_NB - 1, FLAGS, "color" },
  62. { "white", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_WHITE}, 0, 0, FLAGS, "color" },
  63. { "pink", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_PINK}, 0, 0, FLAGS, "color" },
  64. { "brown", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BROWN}, 0, 0, FLAGS, "color" },
  65. { "blue", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_BLUE}, 0, 0, FLAGS, "color" },
  66. { "violet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VIOLET}, 0, 0, FLAGS, "color" },
  67. { "velvet", 0, 0, AV_OPT_TYPE_CONST, {.i64 = NM_VELVET}, 0, 0, FLAGS, "color" },
  68. { "seed", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
  69. { "s", "set random seed", OFFSET(seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT_MAX, FLAGS },
  70. { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
  71. { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
  72. {NULL}
  73. };
  74. AVFILTER_DEFINE_CLASS(anoisesrc);
  75. static av_cold int query_formats(AVFilterContext *ctx)
  76. {
  77. ANoiseSrcContext *s = ctx->priv;
  78. static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
  79. int sample_rates[] = { s->sample_rate, -1 };
  80. static const enum AVSampleFormat sample_fmts[] = {
  81. AV_SAMPLE_FMT_DBL,
  82. AV_SAMPLE_FMT_NONE
  83. };
  84. AVFilterFormats *formats;
  85. AVFilterChannelLayouts *layouts;
  86. int ret;
  87. formats = ff_make_format_list(sample_fmts);
  88. if (!formats)
  89. return AVERROR(ENOMEM);
  90. ret = ff_set_common_formats (ctx, formats);
  91. if (ret < 0)
  92. return ret;
  93. layouts = ff_make_format64_list(chlayouts);
  94. if (!layouts)
  95. return AVERROR(ENOMEM);
  96. ret = ff_set_common_channel_layouts(ctx, layouts);
  97. if (ret < 0)
  98. return ret;
  99. formats = ff_make_format_list(sample_rates);
  100. if (!formats)
  101. return AVERROR(ENOMEM);
  102. return ff_set_common_samplerates(ctx, formats);
  103. }
  104. static double white_filter(double white, double *buf, double ha)
  105. {
  106. return white;
  107. }
  108. static double pink_filter(double white, double *buf, double ha)
  109. {
  110. double pink;
  111. /* http://www.musicdsp.org/files/pink.txt */
  112. buf[0] = 0.99886 * buf[0] + white * 0.0555179;
  113. buf[1] = 0.99332 * buf[1] + white * 0.0750759;
  114. buf[2] = 0.96900 * buf[2] + white * 0.1538520;
  115. buf[3] = 0.86650 * buf[3] + white * 0.3104856;
  116. buf[4] = 0.55000 * buf[4] + white * 0.5329522;
  117. buf[5] = -0.7616 * buf[5] - white * 0.0168980;
  118. pink = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
  119. buf[6] = white * 0.115926;
  120. return pink * 0.11;
  121. }
  122. static double blue_filter(double white, double *buf, double ha)
  123. {
  124. double blue;
  125. /* Same as pink_filter but subtract the offsets rather than add */
  126. buf[0] = 0.0555179 * white - 0.99886 * buf[0];
  127. buf[1] = 0.0750759 * white - 0.99332 * buf[1];
  128. buf[2] = 0.1538520 * white - 0.96900 * buf[2];
  129. buf[3] = 0.3104856 * white - 0.86650 * buf[3];
  130. buf[4] = 0.5329522 * white - 0.55000 * buf[4];
  131. buf[5] = -0.016898 * white + 0.76160 * buf[5];
  132. blue = buf[0] + buf[1] + buf[2] + buf[3] + buf[4] + buf[5] + buf[6] + white * 0.5362;
  133. buf[6] = white * 0.115926;
  134. return blue * 0.11;
  135. }
  136. static double brown_filter(double white, double *buf, double ha)
  137. {
  138. double brown;
  139. brown = ((0.02 * white) + buf[0]) / 1.02;
  140. buf[0] = brown;
  141. return brown * 3.5;
  142. }
  143. static double violet_filter(double white, double *buf, double ha)
  144. {
  145. double violet;
  146. violet = ((0.02 * white) - buf[0]) / 1.02;
  147. buf[0] = violet;
  148. return violet * 3.5;
  149. }
  150. static double velvet_filter(double white, double *buf, double ha)
  151. {
  152. return 2. * ha * ((white > ha) - (white < -ha));
  153. }
  154. static av_cold int config_props(AVFilterLink *outlink)
  155. {
  156. AVFilterContext *ctx = outlink->src;
  157. ANoiseSrcContext *s = ctx->priv;
  158. if (s->seed == -1)
  159. s->seed = av_get_random_seed();
  160. av_lfg_init(&s->c, s->seed);
  161. if (s->duration == 0)
  162. s->infinite = 1;
  163. s->duration = av_rescale(s->duration, s->sample_rate, AV_TIME_BASE);
  164. switch (s->color) {
  165. case NM_WHITE: s->filter = white_filter; break;
  166. case NM_PINK: s->filter = pink_filter; break;
  167. case NM_BROWN: s->filter = brown_filter; break;
  168. case NM_BLUE: s->filter = blue_filter; break;
  169. case NM_VIOLET: s->filter = violet_filter; break;
  170. case NM_VELVET: s->filter = velvet_filter; break;
  171. }
  172. return 0;
  173. }
  174. static int activate(AVFilterContext *ctx)
  175. {
  176. AVFilterLink *outlink = ctx->outputs[0];
  177. ANoiseSrcContext *s = ctx->priv;
  178. AVFrame *frame;
  179. int nb_samples, i;
  180. double *dst;
  181. if (!ff_outlink_frame_wanted(outlink))
  182. return FFERROR_NOT_READY;
  183. if (!s->infinite && s->duration <= 0) {
  184. ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
  185. return 0;
  186. } else if (!s->infinite && s->duration < s->nb_samples) {
  187. nb_samples = s->duration;
  188. } else {
  189. nb_samples = s->nb_samples;
  190. }
  191. if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
  192. return AVERROR(ENOMEM);
  193. dst = (double *)frame->data[0];
  194. for (i = 0; i < nb_samples; i++) {
  195. double white;
  196. white = s->amplitude * ((2 * ((double) av_lfg_get(&s->c) / 0xffffffff)) - 1);
  197. dst[i] = s->filter(white, s->buf, s->amplitude * 0.5);
  198. }
  199. if (!s->infinite)
  200. s->duration -= nb_samples;
  201. frame->pts = s->pts;
  202. s->pts += nb_samples;
  203. return ff_filter_frame(outlink, frame);
  204. }
  205. static const AVFilterPad anoisesrc_outputs[] = {
  206. {
  207. .name = "default",
  208. .type = AVMEDIA_TYPE_AUDIO,
  209. .config_props = config_props,
  210. },
  211. { NULL }
  212. };
  213. AVFilter ff_asrc_anoisesrc = {
  214. .name = "anoisesrc",
  215. .description = NULL_IF_CONFIG_SMALL("Generate a noise audio signal."),
  216. .query_formats = query_formats,
  217. .priv_size = sizeof(ANoiseSrcContext),
  218. .inputs = NULL,
  219. .activate = activate,
  220. .outputs = anoisesrc_outputs,
  221. .priv_class = &anoisesrc_class,
  222. };