avf_avectorscope.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2013 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. * audio to video multimedia vectorscope filter
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/parseutils.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "audio.h"
  31. #include "video.h"
  32. #include "internal.h"
  33. enum VectorScopeMode {
  34. LISSAJOUS,
  35. LISSAJOUS_XY,
  36. MODE_NB,
  37. };
  38. typedef struct AudioVectorScopeContext {
  39. const AVClass *class;
  40. AVFrame *outpicref;
  41. int w, h;
  42. int hw, hh;
  43. int mode;
  44. int contrast[3];
  45. int fade[3];
  46. double zoom;
  47. AVRational frame_rate;
  48. } AudioVectorScopeContext;
  49. #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
  50. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  51. static const AVOption avectorscope_options[] = {
  52. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
  53. { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
  54. { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
  55. { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
  56. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
  57. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
  58. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
  59. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
  60. { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
  61. { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
  62. { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
  63. { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
  64. { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
  65. { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
  66. { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 10, FLAGS },
  67. { NULL }
  68. };
  69. AVFILTER_DEFINE_CLASS(avectorscope);
  70. static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
  71. {
  72. const int linesize = s->outpicref->linesize[0];
  73. uint8_t *dst;
  74. if (s->zoom > 1) {
  75. if (y >= s->h || x >= s->w)
  76. return;
  77. } else {
  78. y = FFMIN(y, s->h - 1);
  79. x = FFMIN(x, s->w - 1);
  80. }
  81. dst = &s->outpicref->data[0][y * linesize + x * 4];
  82. dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
  83. dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
  84. dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
  85. }
  86. static void fade(AudioVectorScopeContext *s)
  87. {
  88. const int linesize = s->outpicref->linesize[0];
  89. int i, j;
  90. if (s->fade[0] || s->fade[1] || s->fade[2]) {
  91. uint8_t *d = s->outpicref->data[0];
  92. for (i = 0; i < s->h; i++) {
  93. for (j = 0; j < s->w*4; j+=4) {
  94. d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
  95. d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
  96. d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
  97. }
  98. d += linesize;
  99. }
  100. }
  101. }
  102. static int query_formats(AVFilterContext *ctx)
  103. {
  104. AVFilterFormats *formats = NULL;
  105. AVFilterChannelLayouts *layout = NULL;
  106. AVFilterLink *inlink = ctx->inputs[0];
  107. AVFilterLink *outlink = ctx->outputs[0];
  108. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  109. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  110. formats = ff_make_format_list(sample_fmts);
  111. if (!formats)
  112. return AVERROR(ENOMEM);
  113. ff_formats_ref(formats, &inlink->out_formats);
  114. ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
  115. ff_channel_layouts_ref(layout, &inlink->out_channel_layouts);
  116. formats = ff_all_samplerates();
  117. if (!formats)
  118. return AVERROR(ENOMEM);
  119. ff_formats_ref(formats, &inlink->out_samplerates);
  120. formats = ff_make_format_list(pix_fmts);
  121. if (!formats)
  122. return AVERROR(ENOMEM);
  123. ff_formats_ref(formats, &outlink->in_formats);
  124. return 0;
  125. }
  126. static int config_input(AVFilterLink *inlink)
  127. {
  128. AVFilterContext *ctx = inlink->dst;
  129. AudioVectorScopeContext *s = ctx->priv;
  130. int nb_samples;
  131. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  132. inlink->partial_buf_size =
  133. inlink->min_samples =
  134. inlink->max_samples = nb_samples;
  135. return 0;
  136. }
  137. static int config_output(AVFilterLink *outlink)
  138. {
  139. AudioVectorScopeContext *s = outlink->src->priv;
  140. outlink->w = s->w;
  141. outlink->h = s->h;
  142. outlink->sample_aspect_ratio = (AVRational){1,1};
  143. outlink->frame_rate = s->frame_rate;
  144. s->hw = s->w / 2;
  145. s->hh = s->h / 2;
  146. return 0;
  147. }
  148. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  149. {
  150. AVFilterContext *ctx = inlink->dst;
  151. AVFilterLink *outlink = ctx->outputs[0];
  152. AudioVectorScopeContext *s = ctx->priv;
  153. const int hw = s->hw;
  154. const int hh = s->hh;
  155. unsigned x, y;
  156. const double zoom = s->zoom;
  157. int i;
  158. if (!s->outpicref || s->outpicref->width != outlink->w ||
  159. s->outpicref->height != outlink->h) {
  160. av_frame_free(&s->outpicref);
  161. s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  162. if (!s->outpicref) {
  163. av_frame_free(&insamples);
  164. return AVERROR(ENOMEM);
  165. }
  166. for (i = 0; i < outlink->h; i++)
  167. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
  168. }
  169. s->outpicref->pts = insamples->pts;
  170. fade(s);
  171. switch (insamples->format) {
  172. case AV_SAMPLE_FMT_S16:
  173. for (i = 0; i < insamples->nb_samples; i++) {
  174. int16_t *src = (int16_t *)insamples->data[0] + i * 2;
  175. if (s->mode == LISSAJOUS) {
  176. x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
  177. y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
  178. } else {
  179. x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
  180. y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
  181. }
  182. draw_dot(s, x, y);
  183. }
  184. break;
  185. case AV_SAMPLE_FMT_FLT:
  186. for (i = 0; i < insamples->nb_samples; i++) {
  187. float *src = (float *)insamples->data[0] + i * 2;
  188. if (s->mode == LISSAJOUS) {
  189. x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
  190. y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
  191. } else {
  192. x = (src[1] * zoom + 1) * hw;
  193. y = (src[0] * zoom + 1) * hh;
  194. }
  195. draw_dot(s, x, y);
  196. }
  197. break;
  198. }
  199. av_frame_free(&insamples);
  200. return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  201. }
  202. static av_cold void uninit(AVFilterContext *ctx)
  203. {
  204. AudioVectorScopeContext *s = ctx->priv;
  205. av_frame_free(&s->outpicref);
  206. }
  207. static const AVFilterPad audiovectorscope_inputs[] = {
  208. {
  209. .name = "default",
  210. .type = AVMEDIA_TYPE_AUDIO,
  211. .config_props = config_input,
  212. .filter_frame = filter_frame,
  213. },
  214. { NULL }
  215. };
  216. static const AVFilterPad audiovectorscope_outputs[] = {
  217. {
  218. .name = "default",
  219. .type = AVMEDIA_TYPE_VIDEO,
  220. .config_props = config_output,
  221. },
  222. { NULL }
  223. };
  224. AVFilter ff_avf_avectorscope = {
  225. .name = "avectorscope",
  226. .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
  227. .uninit = uninit,
  228. .query_formats = query_formats,
  229. .priv_size = sizeof(AudioVectorScopeContext),
  230. .inputs = audiovectorscope_inputs,
  231. .outputs = audiovectorscope_outputs,
  232. .priv_class = &avectorscope_class,
  233. };