avf_avectorscope.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. POLAR,
  37. MODE_NB,
  38. };
  39. enum VectorScopeDraw {
  40. DOT,
  41. LINE,
  42. DRAW_NB,
  43. };
  44. typedef struct AudioVectorScopeContext {
  45. const AVClass *class;
  46. AVFrame *outpicref;
  47. int w, h;
  48. int hw, hh;
  49. int mode;
  50. int draw;
  51. int contrast[4];
  52. int fade[4];
  53. double zoom;
  54. unsigned prev_x, prev_y;
  55. AVRational frame_rate;
  56. } AudioVectorScopeContext;
  57. #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
  58. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  59. static const AVOption avectorscope_options[] = {
  60. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
  61. { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
  62. { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
  63. { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
  64. { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, FLAGS, "mode" },
  65. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  66. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  67. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
  68. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
  69. { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
  70. { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
  71. { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
  72. { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
  73. { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
  74. { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
  75. { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
  76. { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
  77. { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 10, FLAGS },
  78. { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
  79. { "dot", "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
  80. { "line", "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" },
  81. { NULL }
  82. };
  83. AVFILTER_DEFINE_CLASS(avectorscope);
  84. static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
  85. {
  86. const int linesize = s->outpicref->linesize[0];
  87. uint8_t *dst;
  88. if (s->zoom > 1) {
  89. if (y >= s->h || x >= s->w)
  90. return;
  91. } else {
  92. y = FFMIN(y, s->h - 1);
  93. x = FFMIN(x, s->w - 1);
  94. }
  95. dst = &s->outpicref->data[0][y * linesize + x * 4];
  96. dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
  97. dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
  98. dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
  99. dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
  100. }
  101. static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
  102. {
  103. int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
  104. int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
  105. int err = (dx>dy ? dx : -dy) / 2, e2;
  106. for (;;) {
  107. draw_dot(s, x0, y0);
  108. if (x0 == x1 && y0 == y1)
  109. break;
  110. e2 = err;
  111. if (e2 >-dx) {
  112. err -= dy;
  113. x0 += sx;
  114. }
  115. if (e2 < dy) {
  116. err += dx;
  117. y0 += sy;
  118. }
  119. }
  120. }
  121. static void fade(AudioVectorScopeContext *s)
  122. {
  123. const int linesize = s->outpicref->linesize[0];
  124. int i, j;
  125. if (s->fade[0] || s->fade[1] || s->fade[2]) {
  126. uint8_t *d = s->outpicref->data[0];
  127. for (i = 0; i < s->h; i++) {
  128. for (j = 0; j < s->w*4; j+=4) {
  129. d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
  130. d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
  131. d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
  132. d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
  133. }
  134. d += linesize;
  135. }
  136. }
  137. }
  138. static int query_formats(AVFilterContext *ctx)
  139. {
  140. AVFilterFormats *formats = NULL;
  141. AVFilterChannelLayouts *layout = NULL;
  142. AVFilterLink *inlink = ctx->inputs[0];
  143. AVFilterLink *outlink = ctx->outputs[0];
  144. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  145. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  146. int ret;
  147. formats = ff_make_format_list(sample_fmts);
  148. if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
  149. (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
  150. (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
  151. return ret;
  152. formats = ff_all_samplerates();
  153. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  154. return ret;
  155. formats = ff_make_format_list(pix_fmts);
  156. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  157. return ret;
  158. return 0;
  159. }
  160. static int config_input(AVFilterLink *inlink)
  161. {
  162. AVFilterContext *ctx = inlink->dst;
  163. AudioVectorScopeContext *s = ctx->priv;
  164. int nb_samples;
  165. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  166. inlink->partial_buf_size =
  167. inlink->min_samples =
  168. inlink->max_samples = nb_samples;
  169. return 0;
  170. }
  171. static int config_output(AVFilterLink *outlink)
  172. {
  173. AudioVectorScopeContext *s = outlink->src->priv;
  174. outlink->w = s->w;
  175. outlink->h = s->h;
  176. outlink->sample_aspect_ratio = (AVRational){1,1};
  177. outlink->frame_rate = s->frame_rate;
  178. s->prev_x = s->hw = s->w / 2;
  179. s->prev_y = s->hh = s->h / 2;
  180. return 0;
  181. }
  182. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  183. {
  184. AVFilterContext *ctx = inlink->dst;
  185. AVFilterLink *outlink = ctx->outputs[0];
  186. AudioVectorScopeContext *s = ctx->priv;
  187. const int hw = s->hw;
  188. const int hh = s->hh;
  189. unsigned x, y;
  190. unsigned prev_x = s->prev_x, prev_y = s->prev_y;
  191. const double zoom = s->zoom;
  192. int i;
  193. if (!s->outpicref || s->outpicref->width != outlink->w ||
  194. s->outpicref->height != outlink->h) {
  195. av_frame_free(&s->outpicref);
  196. s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  197. if (!s->outpicref) {
  198. av_frame_free(&insamples);
  199. return AVERROR(ENOMEM);
  200. }
  201. for (i = 0; i < outlink->h; i++)
  202. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
  203. }
  204. s->outpicref->pts = insamples->pts;
  205. fade(s);
  206. switch (insamples->format) {
  207. case AV_SAMPLE_FMT_S16:
  208. for (i = 0; i < insamples->nb_samples; i++) {
  209. int16_t *src = (int16_t *)insamples->data[0] + i * 2;
  210. if (s->mode == LISSAJOUS) {
  211. x = ((src[1] - src[0]) * zoom / (float)(UINT16_MAX) + 1) * hw;
  212. y = (1.0 - (src[0] + src[1]) * zoom / (float)UINT16_MAX) * hh;
  213. } else if (s->mode == LISSAJOUS_XY) {
  214. x = (src[1] * zoom / (float)INT16_MAX + 1) * hw;
  215. y = (src[0] * zoom / (float)INT16_MAX + 1) * hh;
  216. } else {
  217. float sx, sy, cx, cy;
  218. sx = src[1] * zoom / (float)INT16_MAX;
  219. sy = src[0] * zoom / (float)INT16_MAX;
  220. cx = sx * sqrtf(1 - 0.5*sy*sy);
  221. cy = sy * sqrtf(1 - 0.5*sx*sx);
  222. x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
  223. y = s->h - s->h * fabsf(cx + cy) * .7;
  224. }
  225. if (s->draw == DOT) {
  226. draw_dot(s, x, y);
  227. } else {
  228. draw_line(s, x, y, prev_x, prev_y);
  229. }
  230. prev_x = x;
  231. prev_y = y;
  232. }
  233. break;
  234. case AV_SAMPLE_FMT_FLT:
  235. for (i = 0; i < insamples->nb_samples; i++) {
  236. float *src = (float *)insamples->data[0] + i * 2;
  237. if (s->mode == LISSAJOUS) {
  238. x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
  239. y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
  240. } else if (s->mode == LISSAJOUS_XY){
  241. x = (src[1] * zoom + 1) * hw;
  242. y = (src[0] * zoom + 1) * hh;
  243. } else {
  244. float sx, sy, cx, cy;
  245. sx = src[1] * zoom;
  246. sy = src[0] * zoom;
  247. cx = sx * sqrtf(1 - 0.5 * sy * sy);
  248. cy = sy * sqrtf(1 - 0.5 * sx * sx);
  249. x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
  250. y = s->h - s->h * fabsf(cx + cy) * .7;
  251. }
  252. if (s->draw == DOT) {
  253. draw_dot(s, x, y);
  254. } else {
  255. draw_line(s, x, y, prev_x, prev_y);
  256. }
  257. prev_x = x;
  258. prev_y = y;
  259. }
  260. break;
  261. default:
  262. av_assert0(0);
  263. }
  264. s->prev_x = x, s->prev_y = y;
  265. av_frame_free(&insamples);
  266. return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  267. }
  268. static av_cold void uninit(AVFilterContext *ctx)
  269. {
  270. AudioVectorScopeContext *s = ctx->priv;
  271. av_frame_free(&s->outpicref);
  272. }
  273. static const AVFilterPad audiovectorscope_inputs[] = {
  274. {
  275. .name = "default",
  276. .type = AVMEDIA_TYPE_AUDIO,
  277. .config_props = config_input,
  278. .filter_frame = filter_frame,
  279. },
  280. { NULL }
  281. };
  282. static const AVFilterPad audiovectorscope_outputs[] = {
  283. {
  284. .name = "default",
  285. .type = AVMEDIA_TYPE_VIDEO,
  286. .config_props = config_output,
  287. },
  288. { NULL }
  289. };
  290. AVFilter ff_avf_avectorscope = {
  291. .name = "avectorscope",
  292. .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
  293. .uninit = uninit,
  294. .query_formats = query_formats,
  295. .priv_size = sizeof(AudioVectorScopeContext),
  296. .inputs = audiovectorscope_inputs,
  297. .outputs = audiovectorscope_outputs,
  298. .priv_class = &avectorscope_class,
  299. };