avf_showvolume.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 "filters.h"
  29. #include "formats.h"
  30. #include "audio.h"
  31. #include "video.h"
  32. #include "internal.h"
  33. static const char *const var_names[] = { "VOLUME", "CHANNEL", "PEAK", NULL };
  34. enum { VAR_VOLUME, VAR_CHANNEL, VAR_PEAK, VAR_VARS_NB };
  35. enum DisplayScale { LINEAR, LOG, NB_DISPLAY_SCALE };
  36. typedef struct ShowVolumeContext {
  37. const AVClass *class;
  38. int w, h;
  39. int b;
  40. double f;
  41. AVRational frame_rate;
  42. char *color;
  43. int orientation;
  44. int step;
  45. float bgopacity;
  46. int mode;
  47. int nb_samples;
  48. AVFrame *out;
  49. AVExpr *c_expr;
  50. int draw_text;
  51. int draw_volume;
  52. double *values;
  53. uint32_t *color_lut;
  54. float *max;
  55. float rms_factor;
  56. int display_scale;
  57. double draw_persistent_duration; /* in second */
  58. uint8_t persistant_max_rgba[4];
  59. int persistent_max_frames; /* number of frames to check max value */
  60. float *max_persistent; /* max value for draw_persistent_max for each channel */
  61. int *nb_frames_max_display; /* number of frame for each channel, for displaying the max value */
  62. void (*meter)(float *src, int nb_samples, float *max, float factor);
  63. } ShowVolumeContext;
  64. #define OFFSET(x) offsetof(ShowVolumeContext, x)
  65. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  66. static const AVOption showvolume_options[] = {
  67. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  68. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, INT_MAX, FLAGS },
  69. { "b", "set border width", OFFSET(b), AV_OPT_TYPE_INT, {.i64=1}, 0, 5, FLAGS },
  70. { "w", "set channel width", OFFSET(w), AV_OPT_TYPE_INT, {.i64=400}, 80, 8192, FLAGS },
  71. { "h", "set channel height", OFFSET(h), AV_OPT_TYPE_INT, {.i64=20}, 1, 900, FLAGS },
  72. { "f", "set fade", OFFSET(f), AV_OPT_TYPE_DOUBLE, {.dbl=0.95}, 0, 1, FLAGS },
  73. { "c", "set volume color expression", OFFSET(color), AV_OPT_TYPE_STRING, {.str="PEAK*255+floor((1-PEAK)*255)*256+0xff000000"}, 0, 0, FLAGS },
  74. { "t", "display channel names", OFFSET(draw_text), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  75. { "v", "display volume value", OFFSET(draw_volume), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
  76. { "dm", "duration for max value display", OFFSET(draw_persistent_duration), AV_OPT_TYPE_DOUBLE, {.dbl=0.}, 0, 9000, FLAGS},
  77. { "dmc","set color of the max value line", OFFSET(persistant_max_rgba), AV_OPT_TYPE_COLOR, {.str = "orange"}, CHAR_MIN, CHAR_MAX, FLAGS },
  78. { "o", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "orientation" },
  79. { "h", "horizontal", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "orientation" },
  80. { "v", "vertical", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "orientation" },
  81. { "s", "set step size", OFFSET(step), AV_OPT_TYPE_INT, {.i64=0}, 0, 5, FLAGS },
  82. { "p", "set background opacity", OFFSET(bgopacity), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, FLAGS },
  83. { "m", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "mode" },
  84. { "p", "peak", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
  85. { "r", "rms", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
  86. { "ds", "set display scale", OFFSET(display_scale), AV_OPT_TYPE_INT, {.i64=LINEAR}, LINEAR, NB_DISPLAY_SCALE - 1, FLAGS, "display_scale" },
  87. { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "display_scale" },
  88. { "log", "log", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "display_scale" },
  89. { NULL }
  90. };
  91. AVFILTER_DEFINE_CLASS(showvolume);
  92. static av_cold int init(AVFilterContext *ctx)
  93. {
  94. ShowVolumeContext *s = ctx->priv;
  95. int ret;
  96. if (s->color) {
  97. ret = av_expr_parse(&s->c_expr, s->color, var_names,
  98. NULL, NULL, NULL, NULL, 0, ctx);
  99. if (ret < 0)
  100. return ret;
  101. }
  102. return 0;
  103. }
  104. static int query_formats(AVFilterContext *ctx)
  105. {
  106. AVFilterFormats *formats = NULL;
  107. AVFilterChannelLayouts *layouts = NULL;
  108. AVFilterLink *inlink = ctx->inputs[0];
  109. AVFilterLink *outlink = ctx->outputs[0];
  110. static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
  111. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
  112. int ret;
  113. formats = ff_make_format_list(sample_fmts);
  114. if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
  115. return ret;
  116. layouts = ff_all_channel_counts();
  117. if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
  118. return ret;
  119. formats = ff_all_samplerates();
  120. if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
  121. return ret;
  122. formats = ff_make_format_list(pix_fmts);
  123. if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
  124. return ret;
  125. return 0;
  126. }
  127. static void find_peak(float *src, int nb_samples, float *peak, float factor)
  128. {
  129. int i;
  130. *peak = 0;
  131. for (i = 0; i < nb_samples; i++)
  132. *peak = FFMAX(*peak, FFABS(src[i]));
  133. }
  134. static void find_rms(float *src, int nb_samples, float *rms, float factor)
  135. {
  136. int i;
  137. for (i = 0; i < nb_samples; i++)
  138. *rms += factor * (src[i] * src[i] - *rms);
  139. }
  140. static int config_input(AVFilterLink *inlink)
  141. {
  142. AVFilterContext *ctx = inlink->dst;
  143. ShowVolumeContext *s = ctx->priv;
  144. s->nb_samples = FFMAX(1, av_rescale(inlink->sample_rate, s->frame_rate.den, s->frame_rate.num));
  145. s->values = av_calloc(inlink->channels * VAR_VARS_NB, sizeof(double));
  146. if (!s->values)
  147. return AVERROR(ENOMEM);
  148. s->color_lut = av_calloc(s->w, sizeof(*s->color_lut) * inlink->channels);
  149. if (!s->color_lut)
  150. return AVERROR(ENOMEM);
  151. s->max = av_calloc(inlink->channels, sizeof(*s->max));
  152. if (!s->max)
  153. return AVERROR(ENOMEM);
  154. s->rms_factor = 10000. / inlink->sample_rate;
  155. switch (s->mode) {
  156. case 0: s->meter = find_peak; break;
  157. case 1: s->meter = find_rms; break;
  158. default: return AVERROR_BUG;
  159. }
  160. if (s->draw_persistent_duration > 0.) {
  161. s->persistent_max_frames = (int) FFMAX(av_q2d(s->frame_rate) * s->draw_persistent_duration, 1.);
  162. s->max_persistent = av_calloc(inlink->channels * s->persistent_max_frames, sizeof(*s->max_persistent));
  163. s->nb_frames_max_display = av_calloc(inlink->channels * s->persistent_max_frames, sizeof(*s->nb_frames_max_display));
  164. }
  165. return 0;
  166. }
  167. static int config_output(AVFilterLink *outlink)
  168. {
  169. ShowVolumeContext *s = outlink->src->priv;
  170. AVFilterLink *inlink = outlink->src->inputs[0];
  171. int ch;
  172. if (s->orientation) {
  173. outlink->h = s->w;
  174. outlink->w = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  175. } else {
  176. outlink->w = s->w;
  177. outlink->h = s->h * inlink->channels + (inlink->channels - 1) * s->b;
  178. }
  179. outlink->sample_aspect_ratio = (AVRational){1,1};
  180. outlink->frame_rate = s->frame_rate;
  181. for (ch = 0; ch < inlink->channels; ch++) {
  182. int i;
  183. for (i = 0; i < s->w; i++) {
  184. float max = i / (float)(s->w - 1);
  185. s->values[ch * VAR_VARS_NB + VAR_PEAK] = max;
  186. s->values[ch * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  187. s->values[ch * VAR_VARS_NB + VAR_CHANNEL] = ch;
  188. s->color_lut[ch * s->w + i] = av_expr_eval(s->c_expr, &s->values[ch * VAR_VARS_NB], NULL);
  189. }
  190. }
  191. return 0;
  192. }
  193. static void drawtext(AVFrame *pic, int x, int y, const char *txt, int o)
  194. {
  195. const uint8_t *font;
  196. int font_height;
  197. int i;
  198. font = avpriv_cga_font, font_height = 8;
  199. for (i = 0; txt[i]; i++) {
  200. int char_y, mask;
  201. if (o) { /* vertical orientation */
  202. for (char_y = font_height - 1; char_y >= 0; char_y--) {
  203. uint8_t *p = pic->data[0] + (y + i * 10) * pic->linesize[0] + x * 4;
  204. for (mask = 0x80; mask; mask >>= 1) {
  205. if (font[txt[i] * font_height + font_height - 1 - char_y] & mask)
  206. AV_WN32(&p[char_y * 4], ~AV_RN32(&p[char_y * 4]));
  207. p += pic->linesize[0];
  208. }
  209. }
  210. } else { /* horizontal orientation */
  211. uint8_t *p = pic->data[0] + y * pic->linesize[0] + (x + i * 8) * 4;
  212. for (char_y = 0; char_y < font_height; char_y++) {
  213. for (mask = 0x80; mask; mask >>= 1) {
  214. if (font[txt[i] * font_height + char_y] & mask)
  215. AV_WN32(p, ~AV_RN32(p));
  216. p += 4;
  217. }
  218. p += pic->linesize[0] - 8 * 4;
  219. }
  220. }
  221. }
  222. }
  223. static void clear_picture(ShowVolumeContext *s, AVFilterLink *outlink)
  224. {
  225. int i, j;
  226. const uint32_t bg = (uint32_t)(s->bgopacity * 255) << 24;
  227. for (i = 0; i < outlink->h; i++) {
  228. uint32_t *dst = (uint32_t *)(s->out->data[0] + i * s->out->linesize[0]);
  229. for (j = 0; j < outlink->w; j++)
  230. AV_WN32A(dst + j, bg);
  231. }
  232. }
  233. static inline int calc_max_draw(ShowVolumeContext *s, AVFilterLink *outlink, float max)
  234. {
  235. float max_val;
  236. if (s->display_scale == LINEAR) {
  237. max_val = max;
  238. } else { /* log */
  239. max_val = av_clipf(0.21 * log10(max) + 1, 0, 1);
  240. }
  241. if (s->orientation) { /* vertical */
  242. return outlink->h - outlink->h * max_val;
  243. } else { /* horizontal */
  244. return s->w * max_val;
  245. }
  246. }
  247. static inline void calc_persistent_max(ShowVolumeContext *s, float max, int channel)
  248. {
  249. /* update max value for persistent max display */
  250. if ((max >= s->max_persistent[channel]) || (s->nb_frames_max_display[channel] >= s->persistent_max_frames)) { /* update max value for display */
  251. s->max_persistent[channel] = max;
  252. s->nb_frames_max_display[channel] = 0;
  253. } else {
  254. s->nb_frames_max_display[channel] += 1; /* incremente display frame count */
  255. }
  256. }
  257. static inline void draw_max_line(ShowVolumeContext *s, int max_draw, int channel)
  258. {
  259. int k;
  260. if (s->orientation) { /* vertical */
  261. uint8_t *dst = s->out->data[0] + max_draw * s->out->linesize[0] + channel * (s->b + s->h) * 4;
  262. for (k = 0; k < s->h; k++) {
  263. memcpy(dst + k * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
  264. }
  265. } else { /* horizontal */
  266. for (k = 0; k < s->h; k++) {
  267. uint8_t *dst = s->out->data[0] + (channel * s->h + channel * s->b + k) * s->out->linesize[0];
  268. memcpy(dst + max_draw * 4, s->persistant_max_rgba, sizeof(s->persistant_max_rgba));
  269. }
  270. }
  271. }
  272. static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
  273. {
  274. AVFilterContext *ctx = inlink->dst;
  275. AVFilterLink *outlink = ctx->outputs[0];
  276. ShowVolumeContext *s = ctx->priv;
  277. const int step = s->step;
  278. int c, j, k, max_draw;
  279. AVFrame *out;
  280. if (!s->out || s->out->width != outlink->w ||
  281. s->out->height != outlink->h) {
  282. av_frame_free(&s->out);
  283. s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  284. if (!s->out) {
  285. av_frame_free(&insamples);
  286. return AVERROR(ENOMEM);
  287. }
  288. clear_picture(s, outlink);
  289. }
  290. s->out->pts = insamples->pts;
  291. if ((s->f < 1.) && (s->f > 0.)) {
  292. for (j = 0; j < outlink->h; j++) {
  293. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0];
  294. const uint32_t alpha = s->bgopacity * 255;
  295. for (k = 0; k < outlink->w; k++) {
  296. dst[k * 4 + 0] = FFMAX(dst[k * 4 + 0] * s->f, 0);
  297. dst[k * 4 + 1] = FFMAX(dst[k * 4 + 1] * s->f, 0);
  298. dst[k * 4 + 2] = FFMAX(dst[k * 4 + 2] * s->f, 0);
  299. dst[k * 4 + 3] = FFMAX(dst[k * 4 + 3] * s->f, alpha);
  300. }
  301. }
  302. } else if (s->f == 0.) {
  303. clear_picture(s, outlink);
  304. }
  305. if (s->orientation) { /* vertical */
  306. for (c = 0; c < inlink->channels; c++) {
  307. float *src = (float *)insamples->extended_data[c];
  308. uint32_t *lut = s->color_lut + s->w * c;
  309. float max;
  310. s->meter(src, insamples->nb_samples, &s->max[c], s->rms_factor);
  311. max = s->max[c];
  312. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  313. max = av_clipf(max, 0, 1);
  314. max_draw = calc_max_draw(s, outlink, max);
  315. for (j = max_draw; j < s->w; j++) {
  316. uint8_t *dst = s->out->data[0] + j * s->out->linesize[0] + c * (s->b + s->h) * 4;
  317. for (k = 0; k < s->h; k++) {
  318. AV_WN32A(&dst[k * 4], lut[s->w - j - 1]);
  319. if (j & step)
  320. j += step;
  321. }
  322. }
  323. if (s->h >= 8 && s->draw_text) {
  324. const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
  325. if (!channel_name)
  326. continue;
  327. drawtext(s->out, c * (s->h + s->b) + (s->h - 10) / 2, outlink->h - 35, channel_name, 1);
  328. }
  329. if (s->draw_persistent_duration > 0.) {
  330. calc_persistent_max(s, max, c);
  331. max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
  332. draw_max_line(s, max_draw, c);
  333. }
  334. }
  335. } else { /* horizontal */
  336. for (c = 0; c < inlink->channels; c++) {
  337. float *src = (float *)insamples->extended_data[c];
  338. uint32_t *lut = s->color_lut + s->w * c;
  339. float max;
  340. s->meter(src, insamples->nb_samples, &s->max[c], s->rms_factor);
  341. max = s->max[c];
  342. s->values[c * VAR_VARS_NB + VAR_VOLUME] = 20.0 * log10(max);
  343. max = av_clipf(max, 0, 1);
  344. max_draw = calc_max_draw(s, outlink, max);
  345. for (j = 0; j < s->h; j++) {
  346. uint8_t *dst = s->out->data[0] + (c * s->h + c * s->b + j) * s->out->linesize[0];
  347. for (k = 0; k < max_draw; k++) {
  348. AV_WN32A(dst + k * 4, lut[k]);
  349. if (k & step)
  350. k += step;
  351. }
  352. }
  353. if (s->h >= 8 && s->draw_text) {
  354. const char *channel_name = av_get_channel_name(av_channel_layout_extract_channel(insamples->channel_layout, c));
  355. if (!channel_name)
  356. continue;
  357. drawtext(s->out, 2, c * (s->h + s->b) + (s->h - 8) / 2, channel_name, 0);
  358. }
  359. if (s->draw_persistent_duration > 0.) {
  360. calc_persistent_max(s, max, c);
  361. max_draw = FFMAX(0, calc_max_draw(s, outlink, s->max_persistent[c]) - 1);
  362. draw_max_line(s, max_draw, c);
  363. }
  364. }
  365. }
  366. av_frame_free(&insamples);
  367. out = av_frame_clone(s->out);
  368. if (!out)
  369. return AVERROR(ENOMEM);
  370. av_frame_make_writable(out);
  371. /* draw volume level */
  372. for (c = 0; c < inlink->channels && s->h >= 8 && s->draw_volume; c++) {
  373. char buf[16];
  374. if (s->orientation) { /* vertical */
  375. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  376. drawtext(out, c * (s->h + s->b) + (s->h - 8) / 2, 2, buf, 1);
  377. } else { /* horizontal */
  378. snprintf(buf, sizeof(buf), "%.2f", s->values[c * VAR_VARS_NB + VAR_VOLUME]);
  379. drawtext(out, FFMAX(0, s->w - 8 * (int)strlen(buf)), c * (s->h + s->b) + (s->h - 8) / 2, buf, 0);
  380. }
  381. }
  382. return ff_filter_frame(outlink, out);
  383. }
  384. static int activate(AVFilterContext *ctx)
  385. {
  386. AVFilterLink *inlink = ctx->inputs[0];
  387. AVFilterLink *outlink = ctx->outputs[0];
  388. ShowVolumeContext *s = ctx->priv;
  389. AVFrame *in = NULL;
  390. int ret;
  391. FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
  392. ret = ff_inlink_consume_samples(inlink, s->nb_samples, s->nb_samples, &in);
  393. if (ret < 0)
  394. return ret;
  395. if (ret > 0)
  396. return filter_frame(inlink, in);
  397. FF_FILTER_FORWARD_STATUS(inlink, outlink);
  398. FF_FILTER_FORWARD_WANTED(outlink, inlink);
  399. return FFERROR_NOT_READY;
  400. }
  401. static av_cold void uninit(AVFilterContext *ctx)
  402. {
  403. ShowVolumeContext *s = ctx->priv;
  404. av_frame_free(&s->out);
  405. av_expr_free(s->c_expr);
  406. av_freep(&s->values);
  407. av_freep(&s->color_lut);
  408. av_freep(&s->max);
  409. }
  410. static const AVFilterPad showvolume_inputs[] = {
  411. {
  412. .name = "default",
  413. .type = AVMEDIA_TYPE_AUDIO,
  414. .config_props = config_input,
  415. },
  416. { NULL }
  417. };
  418. static const AVFilterPad showvolume_outputs[] = {
  419. {
  420. .name = "default",
  421. .type = AVMEDIA_TYPE_VIDEO,
  422. .config_props = config_output,
  423. },
  424. { NULL }
  425. };
  426. AVFilter ff_avf_showvolume = {
  427. .name = "showvolume",
  428. .description = NULL_IF_CONFIG_SMALL("Convert input audio volume to video output."),
  429. .init = init,
  430. .activate = activate,
  431. .uninit = uninit,
  432. .query_formats = query_formats,
  433. .priv_size = sizeof(ShowVolumeContext),
  434. .inputs = showvolume_inputs,
  435. .outputs = showvolume_outputs,
  436. .priv_class = &showvolume_class,
  437. };