avf_avectorscope.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 "filters.h"
  30. #include "formats.h"
  31. #include "audio.h"
  32. #include "video.h"
  33. #include "internal.h"
  34. enum VectorScopeMode {
  35. LISSAJOUS,
  36. LISSAJOUS_XY,
  37. POLAR,
  38. MODE_NB,
  39. };
  40. enum VectorScopeDraw {
  41. DOT,
  42. LINE,
  43. DRAW_NB,
  44. };
  45. enum VectorScopeScale {
  46. LIN,
  47. SQRT,
  48. CBRT,
  49. LOG,
  50. SCALE_NB,
  51. };
  52. typedef struct AudioVectorScopeContext {
  53. const AVClass *class;
  54. AVFrame *outpicref;
  55. int w, h;
  56. int hw, hh;
  57. int mode;
  58. int draw;
  59. int scale;
  60. int contrast[4];
  61. int fade[4];
  62. double zoom;
  63. int swap;
  64. int mirror;
  65. unsigned prev_x, prev_y;
  66. AVRational frame_rate;
  67. int nb_samples;
  68. } AudioVectorScopeContext;
  69. #define OFFSET(x) offsetof(AudioVectorScopeContext, x)
  70. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  71. static const AVOption avectorscope_options[] = {
  72. { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
  73. { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=LISSAJOUS}, 0, MODE_NB-1, FLAGS, "mode" },
  74. { "lissajous", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS}, 0, 0, FLAGS, "mode" },
  75. { "lissajous_xy", "", 0, AV_OPT_TYPE_CONST, {.i64=LISSAJOUS_XY}, 0, 0, FLAGS, "mode" },
  76. { "polar", "", 0, AV_OPT_TYPE_CONST, {.i64=POLAR}, 0, 0, FLAGS, "mode" },
  77. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  78. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  79. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
  80. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="400x400"}, 0, 0, FLAGS },
  81. { "rc", "set red contrast", OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=40}, 0, 255, FLAGS },
  82. { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=160}, 0, 255, FLAGS },
  83. { "bc", "set blue contrast", OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=80}, 0, 255, FLAGS },
  84. { "ac", "set alpha contrast", OFFSET(contrast[3]), AV_OPT_TYPE_INT, {.i64=255}, 0, 255, FLAGS },
  85. { "rf", "set red fade", OFFSET(fade[0]), AV_OPT_TYPE_INT, {.i64=15}, 0, 255, FLAGS },
  86. { "gf", "set green fade", OFFSET(fade[1]), AV_OPT_TYPE_INT, {.i64=10}, 0, 255, FLAGS },
  87. { "bf", "set blue fade", OFFSET(fade[2]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
  88. { "af", "set alpha fade", OFFSET(fade[3]), AV_OPT_TYPE_INT, {.i64=5}, 0, 255, FLAGS },
  89. { "zoom", "set zoom factor", OFFSET(zoom), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 10, FLAGS },
  90. { "draw", "set draw mode", OFFSET(draw), AV_OPT_TYPE_INT, {.i64=DOT}, 0, DRAW_NB-1, FLAGS, "draw" },
  91. { "dot", "", 0, AV_OPT_TYPE_CONST, {.i64=DOT} , 0, 0, FLAGS, "draw" },
  92. { "line", "", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "draw" },
  93. { "scale", "set amplitude scale mode", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LIN}, 0, SCALE_NB-1, FLAGS, "scale" },
  94. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LIN}, 0, 0, FLAGS, "scale" },
  95. { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
  96. { "cbrt", "cube root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
  97. { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
  98. { "swap", "swap x axis with y axis", OFFSET(swap), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  99. { "mirror", "mirror axis", OFFSET(mirror), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, "mirror" },
  100. { "none", "no mirror", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mirror" },
  101. { "x", "mirror x", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mirror" },
  102. { "y", "mirror y", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mirror" },
  103. { "xy", "mirror both", 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, "mirror" },
  104. { NULL }
  105. };
  106. AVFILTER_DEFINE_CLASS(avectorscope);
  107. static void draw_dot(AudioVectorScopeContext *s, unsigned x, unsigned y)
  108. {
  109. const int linesize = s->outpicref->linesize[0];
  110. uint8_t *dst;
  111. if (s->zoom > 1) {
  112. if (y >= s->h || x >= s->w)
  113. return;
  114. } else {
  115. y = FFMIN(y, s->h - 1);
  116. x = FFMIN(x, s->w - 1);
  117. }
  118. dst = &s->outpicref->data[0][y * linesize + x * 4];
  119. dst[0] = FFMIN(dst[0] + s->contrast[0], 255);
  120. dst[1] = FFMIN(dst[1] + s->contrast[1], 255);
  121. dst[2] = FFMIN(dst[2] + s->contrast[2], 255);
  122. dst[3] = FFMIN(dst[3] + s->contrast[3], 255);
  123. }
  124. static void draw_line(AudioVectorScopeContext *s, int x0, int y0, int x1, int y1)
  125. {
  126. int dx = FFABS(x1-x0), sx = x0 < x1 ? 1 : -1;
  127. int dy = FFABS(y1-y0), sy = y0 < y1 ? 1 : -1;
  128. int err = (dx>dy ? dx : -dy) / 2, e2;
  129. for (;;) {
  130. draw_dot(s, x0, y0);
  131. if (x0 == x1 && y0 == y1)
  132. break;
  133. e2 = err;
  134. if (e2 >-dx) {
  135. err -= dy;
  136. x0 += sx;
  137. }
  138. if (e2 < dy) {
  139. err += dx;
  140. y0 += sy;
  141. }
  142. }
  143. }
  144. static void fade(AudioVectorScopeContext *s)
  145. {
  146. const int linesize = s->outpicref->linesize[0];
  147. int i, j;
  148. if (s->fade[0] || s->fade[1] || s->fade[2]) {
  149. uint8_t *d = s->outpicref->data[0];
  150. for (i = 0; i < s->h; i++) {
  151. for (j = 0; j < s->w*4; j+=4) {
  152. d[j+0] = FFMAX(d[j+0] - s->fade[0], 0);
  153. d[j+1] = FFMAX(d[j+1] - s->fade[1], 0);
  154. d[j+2] = FFMAX(d[j+2] - s->fade[2], 0);
  155. d[j+3] = FFMAX(d[j+3] - s->fade[3], 0);
  156. }
  157. d += linesize;
  158. }
  159. }
  160. }
  161. static int query_formats(AVFilterContext *ctx)
  162. {
  163. AVFilterFormats *formats = NULL;
  164. AVFilterChannelLayouts *layout = NULL;
  165. AVFilterLink *inlink = ctx->inputs[0];
  166. AVFilterLink *outlink = ctx->outputs[0];
  167. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
  168. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  169. int ret;
  170. formats = ff_make_format_list(sample_fmts);
  171. if ((ret = ff_formats_ref (formats, &inlink->out_formats )) < 0 ||
  172. (ret = ff_add_channel_layout (&layout, AV_CH_LAYOUT_STEREO )) < 0 ||
  173. (ret = ff_channel_layouts_ref (layout , &inlink->out_channel_layouts)) < 0)
  174. return ret;
  175. formats = ff_all_samplerates();
  176. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  177. return ret;
  178. formats = ff_make_format_list(pix_fmts);
  179. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  180. return ret;
  181. return 0;
  182. }
  183. static int config_input(AVFilterLink *inlink)
  184. {
  185. AVFilterContext *ctx = inlink->dst;
  186. AudioVectorScopeContext *s = ctx->priv;
  187. s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
  188. return 0;
  189. }
  190. static int config_output(AVFilterLink *outlink)
  191. {
  192. AudioVectorScopeContext *s = outlink->src->priv;
  193. outlink->w = s->w;
  194. outlink->h = s->h;
  195. outlink->sample_aspect_ratio = (AVRational){1,1};
  196. outlink->frame_rate = s->frame_rate;
  197. s->prev_x = s->hw = s->w / 2;
  198. s->prev_y = s->hh = s->mode == POLAR ? s->h - 1 : s->h / 2;
  199. return 0;
  200. }
  201. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  202. {
  203. AVFilterContext *ctx = inlink->dst;
  204. AVFilterLink *outlink = ctx->outputs[0];
  205. AudioVectorScopeContext *s = ctx->priv;
  206. const int hw = s->hw;
  207. const int hh = s->hh;
  208. unsigned x, y;
  209. unsigned prev_x = s->prev_x, prev_y = s->prev_y;
  210. double zoom = s->zoom;
  211. int i;
  212. if (!s->outpicref || s->outpicref->width != outlink->w ||
  213. s->outpicref->height != outlink->h) {
  214. av_frame_free(&s->outpicref);
  215. s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  216. if (!s->outpicref) {
  217. av_frame_free(&insamples);
  218. return AVERROR(ENOMEM);
  219. }
  220. s->outpicref->sample_aspect_ratio = (AVRational){1,1};
  221. for (i = 0; i < outlink->h; i++)
  222. memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w * 4);
  223. }
  224. s->outpicref->pts = insamples->pts;
  225. fade(s);
  226. if (zoom < 1) {
  227. float max = 0;
  228. switch (insamples->format) {
  229. case AV_SAMPLE_FMT_S16: {
  230. int16_t *samples = (int16_t *)insamples->data[0];
  231. for (i = 0; i < insamples->nb_samples * 2; i++) {
  232. float sample = samples[i] / (float)INT16_MAX;
  233. max = FFMAX(FFABS(sample), max);
  234. }
  235. }
  236. break;
  237. case AV_SAMPLE_FMT_FLT: {
  238. float *samples = (float *)insamples->data[0];
  239. for (i = 0; i < insamples->nb_samples * 2; i++) {
  240. max = FFMAX(FFABS(samples[i]), max);
  241. }
  242. }
  243. break;
  244. default:
  245. av_assert2(0);
  246. }
  247. zoom = 1. / max;
  248. }
  249. for (i = 0; i < insamples->nb_samples; i++) {
  250. int16_t *samples = (int16_t *)insamples->data[0] + i * 2;
  251. float *samplesf = (float *)insamples->data[0] + i * 2;
  252. float src[2];
  253. switch (insamples->format) {
  254. case AV_SAMPLE_FMT_S16:
  255. src[0] = samples[0] / (float)INT16_MAX;
  256. src[1] = samples[1] / (float)INT16_MAX;
  257. break;
  258. case AV_SAMPLE_FMT_FLT:
  259. src[0] = samplesf[0];
  260. src[1] = samplesf[1];
  261. break;
  262. default:
  263. av_assert2(0);
  264. }
  265. switch (s->scale) {
  266. case SQRT:
  267. src[0] = FFSIGN(src[0]) * sqrtf(FFABS(src[0]));
  268. src[1] = FFSIGN(src[1]) * sqrtf(FFABS(src[1]));
  269. break;
  270. case CBRT:
  271. src[0] = FFSIGN(src[0]) * cbrtf(FFABS(src[0]));
  272. src[1] = FFSIGN(src[1]) * cbrtf(FFABS(src[1]));
  273. break;
  274. case LOG:
  275. src[0] = FFSIGN(src[0]) * logf(1 + FFABS(src[0])) / logf(2);
  276. src[1] = FFSIGN(src[1]) * logf(1 + FFABS(src[1])) / logf(2);
  277. break;
  278. }
  279. if (s->mirror & 1)
  280. src[0] = -src[0];
  281. if (s->mirror & 2)
  282. src[1] = -src[1];
  283. if (s->swap)
  284. FFSWAP(float, src[0], src[1]);
  285. if (s->mode == LISSAJOUS) {
  286. x = ((src[1] - src[0]) * zoom / 2 + 1) * hw;
  287. y = (1.0 - (src[0] + src[1]) * zoom / 2) * hh;
  288. } else if (s->mode == LISSAJOUS_XY) {
  289. x = (src[1] * zoom + 1) * hw;
  290. y = (src[0] * zoom + 1) * hh;
  291. } else {
  292. float sx, sy, cx, cy;
  293. sx = src[1] * zoom;
  294. sy = src[0] * zoom;
  295. cx = sx * sqrtf(1 - 0.5 * sy * sy);
  296. cy = sy * sqrtf(1 - 0.5 * sx * sx);
  297. x = hw + hw * FFSIGN(cx + cy) * (cx - cy) * .7;
  298. y = s->h - s->h * fabsf(cx + cy) * .7;
  299. }
  300. if (s->draw == DOT) {
  301. draw_dot(s, x, y);
  302. } else {
  303. draw_line(s, x, y, prev_x, prev_y);
  304. }
  305. prev_x = x;
  306. prev_y = y;
  307. }
  308. s->prev_x = x, s->prev_y = y;
  309. av_frame_free(&insamples);
  310. return ff_filter_frame(outlink, av_frame_clone(s->outpicref));
  311. }
  312. static int activate(AVFilterContext *ctx)
  313. {
  314. AVFilterLink *inlink = ctx->inputs[0];
  315. AVFilterLink *outlink = ctx->outputs[0];
  316. AudioVectorScopeContext *s = ctx->priv;
  317. AVFrame *in;
  318. int ret;
  319. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  320. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  321. if (ret < 0)
  322. return ret;
  323. if (ret > 0)
  324. return filter_frame(inlink, in);
  325. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  326. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  327. return FFERROR_NOT_READY;
  328. }
  329. static av_cold void uninit(AVFilterContext *ctx)
  330. {
  331. AudioVectorScopeContext *s = ctx->priv;
  332. av_frame_free(&s->outpicref);
  333. }
  334. static const AVFilterPad audiovectorscope_inputs[] = {
  335. {
  336. .name = "default",
  337. .type = AVMEDIA_TYPE_AUDIO,
  338. .config_props = config_input,
  339. },
  340. { NULL }
  341. };
  342. static const AVFilterPad audiovectorscope_outputs[] = {
  343. {
  344. .name = "default",
  345. .type = AVMEDIA_TYPE_VIDEO,
  346. .config_props = config_output,
  347. },
  348. { NULL }
  349. };
  350. AVFilter ff_avf_avectorscope = {
  351. .name = "avectorscope",
  352. .description = NULL_IF_CONFIG_SMALL("Convert input audio to vectorscope video output."),
  353. .uninit = uninit,
  354. .query_formats = query_formats,
  355. .priv_size = sizeof(AudioVectorScopeContext),
  356. .activate = activate,
  357. .inputs = audiovectorscope_inputs,
  358. .outputs = audiovectorscope_outputs,
  359. .priv_class = &avectorscope_class,
  360. };