avf_showvolume.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2015 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. #include "libavutil/avstring.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/parseutils.h"
  26. #include "libavutil/xga_font_data.h"
  27. #include "avfilter.h"
  28. #include "formats.h"
  29. #include "audio.h"
  30. #include "video.h"
  31. #include "internal.h"
  32. static const char *const var_names[] = { "VOLUME", "CHANNEL", NULL };
  33. enum { VAR_VOLUME, VAR_CHANNEL, VAR_VARS_NB };
  34. typedef struct ShowVolumeContext {
  35. const AVClass *class;
  36. int w, h;
  37. int b;
  38. double f;
  39. AVRational frame_rate;
  40. char *color;
  41. int orientation;
  42. int step;
  43. AVFrame *out;
  44. AVExpr *c_expr;
  45. int draw_text;
  46. int draw_volume;
  47. double *values;
  48. } ShowVolumeContext;
  49. #define OFFSET(x) offsetof(ShowVolumeContext, x)
  50. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  51. static const AVOption showvolume_options[] = {
  52. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  53. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  54. { "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
  55. { "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
  56. { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
  57. { "f", "set fade", OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0.001, 1, FLAGS },
  58. { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="if(gte(VOLUME,-6), if(gte(VOLUME,-2), if(gte(VOLUME,-1), if(gt(VOLUME,0), 0xff0000ff, 0xff0066ff), 0xff00ffff),0xff00ff00),0xffff0000)"}, 0, 0, FLAGS },
  59. { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  60. { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  61. { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
  62. { "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
  63. { "v", "vertical", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
  64. { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
  65. { NULL }
  66. };
  67. AVFILTER_DEFINE_CLASS(showvolume);
  68. static av_cold int init(AVFilterContext *ctx)
  69. {
  70. ShowVolumeContext *s = ctx->priv;
  71. int ret;
  72. if (s->color) {
  73. ret = av_expr_parse(&s->c_expr, s->color, var_names,
  74. NULL, NULL, NULL, NULL, 0, ctx);
  75. if (ret < 0)
  76. return ret;
  77. }
  78. return 0;
  79. }
  80. static int query_formats(AVFilterContext *ctx)
  81. {
  82. AVFilterFormats *formats = NULL;
  83. AVFilterChannelLayouts *layouts = NULL;
  84. AVFilterLink *inlink = ctx->inputs[0];
  85. AVFilterLink *outlink = ctx->outputs[0];
  86. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  87. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  88. int ret;
  89. formats = ff_make_format_list(sample_fmts);
  90. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  91. return ret;
  92. layouts = ff_all_channel_counts();
  93. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  94. return ret;
  95. formats = ff_all_samplerates();
  96. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  97. return ret;
  98. formats = ff_make_format_list(pix_fmts);
  99. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  100. return ret;
  101. return 0;
  102. }
  103. static int config_input(AVFilterLink *inlink)
  104. {
  105. AVFilterContext *ctx = inlink->dst;
  106. ShowVolumeContext *s = ctx->priv;
  107. int nb_samples;
  108. nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
  109. inlink->partial_buf_size =
  110. inlink->min_samples =
  111. inlink->max_samples = nb_samples;
  112. s->values = av_calloc(inlink->channels * VAR_VARS_NB, sizeof(double));
  113. if (!s->values)
  114. return AVERROR(ENOMEM);
  115. return 0;
  116. }
  117. static int config_output(AVFilterLink *outlink)
  118. {
  119. ShowVolumeContext *s = outlink->src->priv;
  120. AVFilterLink *inlink = outlink->src->inputs[0];
  121. if (s->orientation) {
  122. outlink->h = s->w;
  123. outlink->w = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  124. } else {
  125. outlink->w = s->w;
  126. outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  127. }
  128. outlink->sample_aspect_ratio = (AVRational){1,1};
  129. outlink->frame_rate = s->frame_rate;
  130. return 0;
  131. }
  132. static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
  133. {
  134. const uint8_t *font;
  135. int font_height;
  136. int i;
  137. font = avpriv_cga_font, font_height = 8;
  138. for (i = 0; txt[i]; i++) {
  139. int char_y, mask;
  140. if (o) {
  141. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  142. uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
  143. for (mask = 0x80; mask; mask >>= 1) {
  144. if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
  145. AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
  146. p += pic->linesize[0];
  147. }
  148. }
  149. } else {
  150. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  151. for (char_y = 0; char_y < font_height; char_y++) {
  152. for (mask = 0x80; mask; mask >>= 1) {
  153. if (font[txt[i] * font_height + char_y] & mask)
  154. AV_WN32(p, ~AV_RN32(p));
  155. p += 4;
  156. }
  157. p += pic->linesize[0] - 8 * 4;
  158. }
  159. }
  160. }
  161. }
  162. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  163. {
  164. AVFilterContext *ctx = inlink->dst;
  165. AVFilterLink *outlink = ctx->outputs[0];
  166. ShowVolumeContext *s = ctx->priv;
  167. const int step = s->step;
  168. int c, i, j, k;
  169. AVFrame *out;
  170. if (!s->out || s->out->width != outlink->w ||
  171. s->out->height != outlink->h) {
  172. av_frame_free(&s->out);
  173. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  174. if (!s->out) {
  175. av_frame_free(&insamples);
  176. return AVERROR(ENOMEM);
  177. }
  178. for (i = 0; i < outlink->h; i++)
  179. memset(s->out->data[0] + i * s->out->linesize[0], 0, outlink->w * 4);
  180. }
  181. s->out->pts = insamples->pts;
  182. for (j = 0; j < outlink->h; j++) {
  183. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
  184. for (k = 0; k < outlink->w; k++) {
  185. dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
  186. dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
  187. dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
  188. dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, 0);
  189. }
  190. }
  191. if (s->orientation) {
  192. for (c = 0; c < inlink->channels; c++) {
  193. float *src = (float *)insamples->extended_data[c];
  194. float max = 0;
  195. uint32_t color;
  196. for (i = 0; i < insamples->nb_samples; i++)
  197. max = FFMAX(max, src[i]);
  198. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  199. max = av_clipf(max, 0, 1);
  200. s->values[c * VAR_VARS_NB + VAR_CHANNEL] = c;
  201. color = av_expr_eval(s->c_expr, &s->values[c * VAR_VARS_NB], NULL);
  202. for (j = outlink->h - outlink->h * max; j < outlink->h; j++) {
  203. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
  204. for (k = 0; k < s->h; k++) {
  205. AV_WN32A(&dst[k * 4], color);
  206. if (j & step)
  207. j += step;
  208. }
  209. }
  210. if (outlink->h > 40 && s->draw_text) {
  211. const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
  212. if (!channel_name)
  213. continue;
  214. drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
  215. }
  216. }
  217. } else {
  218. for (c = 0; c < inlink->channels; c++) {
  219. float *src = (float *)insamples->extended_data[c];
  220. float max = 0;
  221. uint32_t color;
  222. for (i = 0; i < insamples->nb_samples; i++)
  223. max = FFMAX(max, src[i]);
  224. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  225. max = av_clipf(max, 0, 1);
  226. s->values[c * VAR_VARS_NB + VAR_CHANNEL] = c;
  227. color = av_expr_eval(s->c_expr, &s->values[c * VAR_VARS_NB], NULL);
  228. for (j = 0; j < s->h; j++) {
  229. uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
  230. for (k = 0; k < s->w * max; k++) {
  231. AV_WN32A(dst + k * 4, color);
  232. if (k & step)
  233. k += step;
  234. }
  235. }
  236. if (s->h >= 8 && s->draw_text) {
  237. const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
  238. if (!channel_name)
  239. continue;
  240. drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
  241. }
  242. }
  243. }
  244. av_frame_free(&insamples);
  245. out = av_frame_clone(s->out);
  246. if (!out)
  247. return AVERROR(ENOMEM);
  248. av_frame_make_writable(out);
  249. for (c = 0; c < inlink->channels && s->draw_volume; c++) {
  250. char buf[16];
  251. if (s->orientation) {
  252. if (s->h >= 8) {
  253. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  254. drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
  255. }
  256. } else {
  257. if (s->h >= 8) {
  258. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  259. drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
  260. }
  261. }
  262. }
  263. return ff_filter_frame(outlink, out);
  264. }
  265. static av_cold void uninit(AVFilterContext *ctx)
  266. {
  267. ShowVolumeContext *s = ctx->priv;
  268. av_frame_free(&s->out);
  269. av_expr_free(s->c_expr);
  270. av_freep(&s->values);
  271. }
  272. static const AVFilterPad showvolume_inputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_AUDIO,
  276. .config_props = config_input,
  277. .filter_frame = filter_frame,
  278. },
  279. { NULL }
  280. };
  281. static const AVFilterPad showvolume_outputs[] = {
  282. {
  283. .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .config_props = config_output,
  286. },
  287. { NULL }
  288. };
  289. AVFilter ff_avf_showvolume = {
  290. .name = "showvolume",
  291. .description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
  292. .init = init,
  293. .uninit = uninit,
  294. .query_formats = query_formats,
  295. .priv_size = sizeof(ShowVolumeContext),
  296. .inputs = showvolume_inputs,
  297. .outputs = showvolume_outputs,
  298. .priv_class = &showvolume_class,
  299. };